Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. // In browsers _this is set to window in the global scope
  2. console.log(this === window);
  3.  
  4. (function(){
  5. console.log(this === window)
  6.  
  7. var myAwesomeFunction = function(name, age){
  8. // This will be whatever you want it to be
  9. console.log(this, name, age);
  10. };
  11.  
  12. myAwesomeFunction.call({a: 10}, 'Ruby', 100);
  13. // BTW: use apply if you want to pass in an array of arguments
  14. myAwesomeFunction.apply({a: 10}, ['Ruby', 200]);
  15.  
  16. /**
  17. * @param {String} name
  18. * @param {Number} age
  19. *
  20. * @constructor
  21. */
  22. var Person = function Person(name, age) {
  23. /**
  24. *
  25. * @type {String}
  26. */
  27. this.name = name;
  28.  
  29. /**
  30. *
  31. * @type {Number}
  32. */
  33. this.age = age;
  34.  
  35. // JS will automatically set this for constructors ...
  36. console.log(this);
  37. };
  38.  
  39. Person.prototype.sayHi = function() {
  40. // ... or for methods
  41. console.log(this);
  42. };
  43.  
  44. var me = new Person('Ruby', 300);
  45. me.sayHi();
  46. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement