[javascript/en] Minor changes to formatting, prototype explanations

- Made a comment a complete sentence, to match the style of the rest of
  the document.
- Removed discussion of for/in over Arrays, since it's a can of worms,
  and for/of is more appropriate if ES6 operators are available.
- Reworded introduction to prototypes, and moved it with the rest of the
  prototype documentation.
This commit is contained in:
Adam Brenecki 2017-03-01 19:29:32 +10:30
parent 21652477c2
commit 54eff5591a

View File

@ -138,7 +138,7 @@ undefined; // used to indicate a value is not currently present (although
// Note that 0 is falsy and "0" is truthy, even though 0 == "0". // Note that 0 is falsy and "0" is truthy, even though 0 == "0".
/////////////////////////////////// ///////////////////////////////////
// 2. Variables, Arrays and basics of Objects // 2. Variables, Arrays and Objects
// Variables are declared with the `var` keyword. JavaScript is dynamically // Variables are declared with the `var` keyword. JavaScript is dynamically
// typed, so you don't need to specify type. Assignment uses a single `=` // typed, so you don't need to specify type. Assignment uses a single `=`
@ -200,11 +200,6 @@ myObj.myThirdKey = true;
// If you try to access a value that's not yet set, you'll get undefined. // If you try to access a value that's not yet set, you'll get undefined.
myObj.myFourthKey; // = undefined myObj.myFourthKey; // = undefined
// JavaScript objects are not instantiated from blueprints ("classes" in other
// object-oriented languages), but may refer to another special object called a
// "prototype". See later section for more info.
/////////////////////////////////// ///////////////////////////////////
// 3. Logic and Control Structures // 3. Logic and Control Structures
@ -253,23 +248,6 @@ for (var x in person){
description += person[x] + " "; description += person[x] + " ";
} // description = 'Paul Ken 18 ' } // description = 'Paul Ken 18 '
// It also works for array objects but iterates over the indices
var myArray = [1, 2, 3, "apple", "banana", function(){}]
for (var index in myArray) {
console.log(index + ": " + myArray[index]);
}
///prints:
// 0: 1
// 1: 2
// 2: 3
// 3: apple
// 4: banana
// 5: function () {}
// 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.
// && is logical and, || is logical or // && is logical and, || is logical or
if (house.size == "big" && house.colour == "blue"){ if (house.size == "big" && house.colour == "blue"){
house.contains = "bear"; house.contains = "bear";
@ -456,6 +434,10 @@ var MyConstructor = function(){
myNewObj = new MyConstructor(); // = {myNumber: 5} myNewObj = new MyConstructor(); // = {myNumber: 5}
myNewObj.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 // 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 // on an object that doesn't exist on the actual object, the interpreter will
// look at its prototype. // look at its prototype.