Skip to content

Commit 53b6f94

Browse files
committed
Merge pull request #155 from steverob/patch-4
updated lesson 2 in JS
2 parents 13f8e75 + 354d7fe commit 53b6f94

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

js/lesson2/tutorial.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: page
33
title: Beginning JavaScript
44
---
55

6-
In our previous lesson we briefly introduced JavaScript, variables. This time we will be explaining Loops, Arrays, Objects and the DOM.
6+
In our previous lesson we briefly introduced JavaScript - variables, expressions, conditions, etc. This time we will be explaining loops, arrays, objects and the DOM.
77

88
## Before we start...
99

@@ -256,6 +256,9 @@ Here is the basic template for creating an object with some properties
256256
var object = {
257257
propertyName: propertyValue,
258258
propertyName: propertyValue,
259+
methodName: function(){
260+
//method definition
261+
},
259262
...
260263
};
261264
```
@@ -273,7 +276,10 @@ var london = {
273276
numberOfUniversities: 43,
274277
averageRent: 1106,
275278
dailyTubePassengerJourney: 3500000,
276-
olympics: [ 1908, 1948, 2012]
279+
olympics: [ 1908, 1948, 2012],
280+
updatePopulation: function(newPopulation) {
281+
this.population = newPopulation;
282+
}
277283
};
278284
```
279285

@@ -300,6 +306,15 @@ for (i = 0; i < london.olympics.length; i = i + 1) {
300306
console.log(london.olympics[i]);
301307
}
302308
```
309+
Also you can see that we can have methods in them. We have a method `updatePopulation` using which you can update the `population` property of `london`.
310+
311+
```js
312+
console.log("Population before update: " + london.population);
313+
london.updatePopulation(8400000);
314+
console.log("Population after update: " + london.population);
315+
```
316+
We have used the keyword `this` inside the `updatePopulation` method. It is used to access the properties and methods of objects from inside the object itself.
317+
303318

304319
## The DOM
305320

@@ -387,15 +402,15 @@ There are three main steps we need to follow to achieve this.
387402

388403
1. creating an element
389404
```js
390-
document.createElement(<tagName>);
405+
document.createElement("<tagName>");
391406
```
392407
2. creating text nodes
393408
```js
394-
document.createTextNode(<text>);
409+
document.createTextNode("<text>");
395410
```
396411
3. adding children to elements
397412
```js
398-
document.appendChild(<node>);
413+
document.appendChild("<node>");
399414
```
400415

401416
Try this out using the london object we declared previously
@@ -411,7 +426,10 @@ var london = {
411426
numberOfUniversities: 43,
412427
averageRent: 1106,
413428
dailyTubePassengerJourney: 3500000,
414-
olympics: [ 1908, 1948, 2012]
429+
olympics: [ 1908, 1948, 2012],
430+
updatePopulation: function(newPopulation) {
431+
this.population = newPopulation;
432+
}
415433
};
416434
```
417435

0 commit comments

Comments
 (0)