diff options
author | Adam Brenecki <adam@brenecki.id.au> | 2017-03-01 19:32:36 +1030 |
---|---|---|
committer | Adam Brenecki <adam@brenecki.id.au> | 2017-03-01 19:32:36 +1030 |
commit | 1fb8b49fa4f79c81f5adf184188f7e001b235163 (patch) | |
tree | 8d8ab3691f1f7bdddb117f156993589b29e33923 | |
parent | eba9d9ffe54de39ebceb07769d1151ac3d0f5e14 (diff) | |
parent | 54eff5591a1701b40af8632b6c9239dbb8a68ddc (diff) |
Merge branch 'scorpiodawg-master'
-rw-r--r-- | javascript.html.markdown | 56 |
1 files changed, 34 insertions, 22 deletions
diff --git a/javascript.html.markdown b/javascript.html.markdown index 38119864..a85a7872 100644 --- a/javascript.html.markdown +++ b/javascript.html.markdown @@ -225,7 +225,7 @@ do { } while (!isValid(input)) // The `for` loop is the same as C and Java: -// initialisation; continue condition; iteration. +// initialization; continue condition; iteration. for (var i = 0; i < 5; i++){ // will run 5 times } @@ -241,26 +241,12 @@ for (var i = 0; i < 10; i++) { } } -// The for/in statement iterates over every property across the entire prototype chain. +// The for/in statement allows iteration over properties of an object. var description = ""; var person = {fname:"Paul", lname:"Ken", age:18}; for (var x in person){ description += person[x] + " "; -} - -// To only consider properties attached to the object itself -// and not its prototypes, use the `hasOwnProperty()` check. -var description = ""; -var person = {fname:"Paul", lname:"Ken", age:18}; -for (var x in person){ - if (person.hasOwnProperty(x)){ - description += person[x] + " "; - } -} - -// For/in should not be used to iterate over an Array where the index order -// is important, as there is no guarantee that for/in will return the indexes -// in any particular order. +} // description = 'Paul Ken 18 ' // && is logical and, || is logical or if (house.size == "big" && house.colour == "blue"){ @@ -273,7 +259,6 @@ if (colour == "red" || colour == "blue"){ // && and || "short circuit", which is useful for setting default values. var name = otherName || "default"; - // The `switch` statement checks for equality with `===`. // Use 'break' after each case // or the cases after the correct one will be executed too. @@ -449,6 +434,10 @@ var MyConstructor = function(){ myNewObj = new MyConstructor(); // = {myNumber: 5} myNewObj.myNumber; // = 5 +// Unlike most other popular object-oriented languages, JavaScript has no +// concept of 'instances' created from 'class' blueprints; instead, JavaScript +// combines instantiation and inheritance into a single concept: a 'prototype'. + // Every JavaScript object has a 'prototype'. When you go to access a property // on an object that doesn't exist on the actual object, the interpreter will // look at its prototype. @@ -485,6 +474,26 @@ myObj.myBoolean; // = true myPrototype.meaningOfLife = 43; myObj.meaningOfLife; // = 43 +// The for/in statement allows iteration over properties of an object, +// walking up the prototype chain until it sees a null prototype. +for (var x in myObj){ + console.log(myObj[x]); +} +///prints: +// Hello world! +// 42 +// [Function: myFunc] + +// To only consider properties attached to the object itself +// and not its prototypes, use the `hasOwnProperty()` check. +for (var x in myObj){ + if (myObj.hasOwnProperty(x)){ + console.log(myObj[x]); + } +} +///prints: +// Hello world! + // We mentioned that `__proto__` was non-standard, and there's no standard way to // change the prototype of an existing object. However, there are two ways to // create a new object with a given prototype. @@ -570,12 +579,15 @@ of the language. [JavaScript: The Definitive Guide][6] is a classic guide and reference book. -[Eloquent Javascript][8] by Marijn Haverbeke is an excellent JS book/ebook with attached terminal - -[Eloquent Javascript - The Annotated Version][9] by Gordon Zhu is also a great derivative of Eloquent Javascript with extra explanations and clarifications for some of the more complicated examples. +[Eloquent Javascript][8] by Marijn Haverbeke is an excellent JS book/ebook with +attached terminal -[Javascript: The Right Way][10] is a guide intended to introduce new developers to JavaScript and help experienced developers learn more about its best practices. +[Eloquent Javascript - The Annotated Version][9] by Gordon Zhu is also a great +derivative of Eloquent Javascript with extra explanations and clarifications for +some of the more complicated examples. +[Javascript: The Right Way][10] is a guide intended to introduce new developers +to JavaScript and help experienced developers learn more about its best practices. In addition to direct contributors to this article, some content is adapted from Louie Dinh's Python tutorial on this site, and the [JS Tutorial][7] on the |