JavaScript 变量

"use strict";

'use strict';

严格模式使 JavaScript 更加严格,以确保你的最佳习惯。例如,分配变量:

"use strict"; // or 'use strict';
var syntax101 = "var is used when assigning a variable.";
uhOh = "This is an error!";

uhOh 应该使用 var 来定义。严格模式,打开,显示错误(在控制台中,它不关心)。用它来产生定义变量的好习惯。

你可以使用嵌套数组和对象。它们有时很有用,而且它们也很有趣。以下是它们的工作原理:

嵌套数组

var myArray = [ "The following is an array", ["I'm an array"] ];

console.log(myArray[1]); // (1) ["I'm an array"]
console.log(myArray[1][0]); // "I'm an array"

var myGraph = [ [0, 0], [5, 10], [3, 12] ]; // useful nested array

console.log(myGraph[0]); // [0, 0]
console.log(myGraph[1][1]); // 10

嵌套对象

var myObject = {
    firstObject: {
        myVariable: "This is the first object"
    }
    secondObject: {
        myVariable: "This is the second object"
    }
}

console.log(myObject.firstObject.myVariable); // This is the first object.
console.log(myObject.secondObject); // myVariable: "This is the second object"

var people = {
    john: {
        name: {
            first: "John",
            last: "Doe",
            full: "John Doe"
        },
        knownFor: "placeholder names"
    },
    bill: {
        name: {
            first: "Bill",
            last: "Gates",
            full: "Bill Gates"
        },
        knownFor: "wealth"
    }
}

console.log(people.john.name.first); // John
console.log(people.john.name.full); // John Doe
console.log(people.bill.knownFor); // wealth
console.log(people.bill.name.last); // Gates
console.log(people.bill.name.full); // Bill Gates