From e685670133a99a0210eac3ad66425d6a895faf40 Mon Sep 17 00:00:00 2001 From: Markus Amalthea Magnuson Date: Thu, 18 Apr 2013 22:21:04 +0200 Subject: [PATCH 1/2] Fix and format some code comments. --- page/javascript-101/this-keyword.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/page/javascript-101/this-keyword.md b/page/javascript-101/this-keyword.md index 25ae3665..0260d038 100644 --- a/page/javascript-101/this-keyword.md +++ b/page/javascript-101/this-keyword.md @@ -14,7 +14,8 @@ In JavaScript, as in most object-oriented programming languages, `this` is a spe - Otherwise, the function is being invoked as a standalone function not attached to any object, and `this` will refer to the global object. ``` -// A function invoked using Function.call +// A function invoked using Function.call() + var myObject = { sayHello: function() { console.log( "Hi! My name is " + this.myName ); @@ -31,7 +32,8 @@ myObject.sayHello.call( secondObject ); // "Hi! My name is Colin" ``` ``` -// A function created using Function.bind +// A function created using Function.bind() + var myName = "the global object"; var sayHello = function() { console.log( "Hi! My name is " + this.myName ); @@ -41,12 +43,13 @@ var myObject = { }; var myObjectHello = sayHello.bind( myObject ); -sayHello(); // "Hi! My name is the global object" +sayHello(); // "Hi! My name is the global object" myObjectHello(); // "Hi! My name is Rebecca" ``` ``` // A function being attached to an object at runtime. + var myName = "the global object"; var sayHello = function() { console.log( "Hi! My name is " + this.myName ); From 9eb96521145ba8d186dd1a56df8a09586ced7800 Mon Sep 17 00:00:00 2001 From: Markus Amalthea Magnuson Date: Thu, 18 Apr 2013 22:21:31 +0200 Subject: [PATCH 2/2] Parentheses on method mentions. --- page/javascript-101/this-keyword.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/page/javascript-101/this-keyword.md b/page/javascript-101/this-keyword.md index 0260d038..7ce95be3 100644 --- a/page/javascript-101/this-keyword.md +++ b/page/javascript-101/this-keyword.md @@ -8,8 +8,8 @@ attribution: In JavaScript, as in most object-oriented programming languages, `this` is a special keyword that is used in methods to refer to the object on which a method is being invoked. The value of `this` is determined using a simple series of steps: -- If the function is invoked using `Function.call` or `Function.apply`, this will be set to the first argument passed to `.call()`/`.apply()`. If the first argument passed to `.call()`/`.apply()` is `null` or `undefined`, `this` will refer to the global object (which is the `window` object in web browsers). -- If the function being invoked was created using `Function.bind`, `this` will be the first argument that was passed to `.bind()` at the time the function was created. +- If the function is invoked using `Function.call()` or `Function.apply()`, this will be set to the first argument passed to `.call()`/`.apply()`. If the first argument passed to `.call()`/`.apply()` is `null` or `undefined`, `this` will refer to the global object (which is the `window` object in web browsers). +- If the function being invoked was created using `Function.bind()`, `this` will be the first argument that was passed to `.bind()` at the time the function was created. - If the function is being invoked as a method of an object, `this` will refer to that object. - Otherwise, the function is being invoked as a standalone function not attached to any object, and `this` will refer to the global object.