You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: js/lesson2/tutorial.md
+24-6Lines changed: 24 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ layout: page
3
3
title: Beginning JavaScript
4
4
---
5
5
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.
7
7
8
8
## Before we start...
9
9
@@ -256,6 +256,9 @@ Here is the basic template for creating an object with some properties
256
256
var object = {
257
257
propertyName: propertyValue,
258
258
propertyName: propertyValue,
259
+
methodName:function(){
260
+
//method definition
261
+
},
259
262
...
260
263
};
261
264
```
@@ -273,7 +276,10 @@ var london = {
273
276
numberOfUniversities:43,
274
277
averageRent:1106,
275
278
dailyTubePassengerJourney:3500000,
276
-
olympics: [ 1908, 1948, 2012]
279
+
olympics: [ 1908, 1948, 2012],
280
+
updatePopulation:function(newPopulation) {
281
+
this.population= newPopulation;
282
+
}
277
283
};
278
284
```
279
285
@@ -300,6 +306,15 @@ for (i = 0; i < london.olympics.length; i = i + 1) {
300
306
console.log(london.olympics[i]);
301
307
}
302
308
```
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
+
303
318
304
319
## The DOM
305
320
@@ -387,15 +402,15 @@ There are three main steps we need to follow to achieve this.
387
402
388
403
1. creating an element
389
404
```js
390
-
document.createElement(<tagName>);
405
+
document.createElement("<tagName>");
391
406
```
392
407
2. creating text nodes
393
408
```js
394
-
document.createTextNode(<text>);
409
+
document.createTextNode("<text>");
395
410
```
396
411
3. adding children to elements
397
412
```js
398
-
document.appendChild(<node>);
413
+
document.appendChild("<node>");
399
414
```
400
415
401
416
Try this out using the london object we declared previously
0 commit comments