Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // In browsers _this is set to window in the global scope
- console.log(this === window);
- (function(){
- console.log(this === window)
- var myAwesomeFunction = function(name, age){
- // This will be whatever you want it to be
- console.log(this, name, age);
- };
- myAwesomeFunction.call({a: 10}, 'Ruby', 100);
- // BTW: use apply if you want to pass in an array of arguments
- myAwesomeFunction.apply({a: 10}, ['Ruby', 200]);
- /**
- * @param {String} name
- * @param {Number} age
- *
- * @constructor
- */
- var Person = function Person(name, age) {
- /**
- *
- * @type {String}
- */
- this.name = name;
- /**
- *
- * @type {Number}
- */
- this.age = age;
- // JS will automatically set this for constructors ...
- console.log(this);
- };
- Person.prototype.sayHi = function() {
- // ... or for methods
- console.log(this);
- };
- var me = new Person('Ruby', 300);
- me.sayHi();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement