Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //which of obj1 and obj2 will have a func2 method?
  2. //Explain why obj2.prototype is undefined.
  3.  
  4. function func1(){
  5.     this.func2 = function(){
  6.         return this;
  7.     }
  8. }
  9.  
  10. var obj1 = func1();
  11. var obj2 = new func1();
  12.  
  13. console.log(obj1);           // undefined
  14. console.log(obj2);           // {func2 : [Function]}
  15. console.log(obj2.prototype); // undefined
  16.  
  17. /*obj2 will have a func2 method because word new calls a function func1 as a constructor*/
  18. /*all functions have a prototype by default, but func1 becomes a constructor since
  19. it is invoked using word new, therefor obj2 cannot have a prototype. Also prototype property
  20. of an object is not directly accessible and should be set with Object.setPrototypeOf method*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement