Skip to content

Commit 8c2242c

Browse files
committed
converted the 'this keyword' into the new code convention
1 parent 23292b7 commit 8c2242c

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

page/javascript-101/this-keyword.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ of steps:
2020
- Otherwise, the function is being invoked as a standalone function not
2121
attached to any object, and `this` will refer to the global object.
2222

23-
<javascript caption="A function invoked using Function.call">
23+
``` js
24+
// A function invoked using Function.call
2425
var myObject = {
2526
sayHello : function() {
2627
console.log('Hi! My name is ' + this.myName);
@@ -35,9 +36,10 @@ var secondObject = {
3536

3637
myObject.sayHello(); // logs 'Hi! My name is Rebecca'
3738
myObject.sayHello.call(secondObject); // logs 'Hi! My name is Colin'
38-
</javascript>
39+
```
3940

40-
<javascript caption="A function created using Function.bind">
41+
``` js
42+
// A function created using Function.bind
4143
var myName = 'the global object',
4244

4345
sayHello = function () {
@@ -52,9 +54,10 @@ var myObjectHello = sayHello.bind(myObject);
5254

5355
sayHello(); // logs 'Hi! My name is the global object'
5456
myObjectHello(); // logs 'Hi! My name is Rebecca'
55-
</javascript>
57+
```
5658

57-
<javascript caption="A function being attached to an object at runtime">
59+
``` js
60+
// A function being attached to an object at runtime
5861
var myName = 'the global object',
5962

6063
sayHello = function() {
@@ -75,17 +78,17 @@ myObjectHello(); // logs 'Hi! My name is Rebecca'
7578
sayHello(); // logs 'Hi! My name is the global object'
7679
myObject.sayHello(); // logs 'Hi! My name is Rebecca'
7780
secondObject.sayHello(); // logs 'Hi! My name is Colin'
78-
</javascript>
81+
```
7982

80-
<div class="note" markdown="1">
83+
<div class="note">
8184
When invoking a function deep within a long namespace, it is often tempting to
8285
reduce the amount of code you need to type by storing a reference to the actual
8386
function as a single, shorter variable. It is important not to do this with
8487
instance methods as this will cause the value of `this` within the function to
8588
change, leading to incorrect code operation. For instance:
8689
</div>
8790

88-
<javascript>
91+
``` js
8992
var myNamespace = {
9093
myObject : {
9194
sayHello : function() {
@@ -99,14 +102,14 @@ var myNamespace = {
99102
var hello = myNamespace.myObject.sayHello;
100103

101104
hello(); // logs 'Hi! My name is undefined'
102-
</javascript>
105+
```
103106

104107

105-
<div class="note" markdown="1">
108+
<div class="note">
106109
You can, however, safely reduce everything up to the object on which the method is invoked:
107110
</div>
108111

109-
<javascript>
112+
```
110113
var myNamespace = {
111114
myObject : {
112115
sayHello : function() {
@@ -120,4 +123,4 @@ var myNamespace = {
120123
var obj = myNamespace.myObject;
121124
122125
obj.sayHello(); // logs 'Hi! My name is Rebecca'
123-
</javascript>
126+
```

0 commit comments

Comments
 (0)