ES5

Strict

"use strict";

Object方法

Object.create()

var a=Object.create(b) //会把b对象加到a.proto 上 实现继承

更精细的控制

    var FantasticFour = Object.create(
        marvelProto,
        {
            title: {
                value: "Fantastic Four",
                enumerable: true,
                writable: false,
                configurable: false
            },
            year: {
                value: "1961",
                enumerable: true,
                writable: false,
                configurable: false
            }
        }
    );
    console.log(
        FantasticFour.title
        + " was first published by " +
        FantasticFour.publisher
        + " in " +
        FantasticFour.year
    );

Object.preventExtensions() 和 Object.isExtensible()

    //Let's see if we can add some more info to the object
    if (Object.isExtensible(marvelProto)){
        //true
        marvelProto.flagshipCharater = "Spider-Man";
    };

    //Stop extensions
    Object.preventExtensions(marvelProto);

    //Adding an Editor in Chief field throws an error
    marvelProto.editorInChief = "Axel Alonso";
    >>>TypeError: Can't add property editorInChief, object is not extensible

Object.seal() 和 Object.isSealed()

Object.seal 首先调用 Object.preventExtensions() ,阻止添加新特性,然后将所有对象特性的 configurable 标志设置为 false。

    //Can we delete configurable properties?
    if (!Object.isSealed(marvelProto)){
        //we can
        delete marvelProto.flagshipCharacter;
    };

    //Seal the object
    Object.seal(marvelProto);

    //deleting the Editor in Chief field throws an error
    delete marvelProto.editorInChief;
    >>>Error: property "use strict";marvelProto.editorInChief is 
    non-configurable and can't be deleted

Object.freeze() 和 Object.isFrozen()

Object.freeze 调用 Object.seal() 来停止对象的配置, 然后将所有对象特性的 writeable 标志设置为 false,提供一个完美静态的对象。

    //Can we write writeable properties?
    if (!Object.isFrozen(marvelProto)){
        //we can
        marvelProto.flagshipCharacter = "Iron Man";
    };

    //Seal the object
    Object.freeze(marvelProto);

    //Changing the Flagship Character throws an error
    marvelProto.flagshipCharacter = "Wolverine";
    >>>Error: "use strict";marvelProto.flagshipCharacter is read-only

Object.getPrototypeOf()

新方法 Object.getPrototypeOf() 返回对象的原型。该方法的值等同于非标准的 Object.proto 特性。

Object.keys() 和 Object.getOwnPropertyNames()

Object.keys()返回enumerable为true的属性。Object.getOwnPropertyNames() 与上述方法类似,但还包含 enumerable 标志设置为 false 的特性。都忽略继承的属性

    //BUT... keys returns ONLY properties on object itself
    //inherited properties are ignored
    //launchComic (enumerable:false) is also skipped
    console.log(Object.keys(marvelProto));
    >>>["publisher", "founded", "founder", "headquarters"]

    //getOwnPropertyNames also operates only on 
    //properties of the object itself, but it
    //also includes properties that have the
    //enumerable flag set to false 
    console.log(Object.getOwnPropertyNames(marvelProto);
    >>>["launchComic", "founder", "founded", "headquarters", "publisher"]

getters 和 setters

Get 和 set 将一个对象特性绑定到一个函数,该函数将在试图访问或写入一个属性的值时被调用。Get 不接受参数;而 set 接受一个参数(要设置的值)。

    var FantasticFour = Object.create(
        marvelProto,
        {
            title : {
                value : "Fantastic Four",
        
            },
            year : {
                value : "1961",
        
            }
        }
    );

    //Use Object.defineProperty to set the getters and setters
    //Alternatively, this could be set in the Object.create above
    Object.defineProperty(FantasticFour, "bestIssue", {
        get: function () { return fave; },
        set: function (num) {
            fave = "The best single issue of Fantastic Four is issue #" + num;
        }
    }
    );

    FantasticFour.bestIssue = 51;
    console.log(FantasticFour.bestIssue);
    >>>The best single issue of Fantastic Four is #51

Object.defineProperty()

Array方法

Array.forEach()

将对数组中的每个元素执行一次的函数

    var arr = [8, 10, 13, 10, 8, 1, 5];
    function logger(element, index, array) {
        console.log("The value of the element at index " + index + " is " + element);
    }
    arr.forEach(logger);

Array.map()

返回一个新数组,新数组通过在原始数组中的每个元素上调用单个函数参数生成

    var arr = [8, 10, 13, 10, 8, 1, 5];
    function square(num){
        return num * num;    
    }    
    console.log(arr.map(square));  
    >>>[64, 100, 169, 100, 64, 1, 25]

Array.reduce() 和 Array.reduceRight()

    var arr = [8, 10, 13, 10, 8, 1, 5];

    console.log(arr.reduce(function(a, b){ return  a + b; }));           
    >>>55

    console.log(arr.reduce(function(a, b){ return  a +" "+ b; }));               
    >>>8 10 13 10 8 1 5

    console.log(arr.reduceRight(function(a, b){ return  a + b; }));           
    >>>55

    console.log(arr.reduceRight(function(a, b){ return  a +" "+ b; }));
    >>>5 1 8 10 13 10 8

Array.filter()

    var arr = [8, 10, 13, 10, 8, 1, 5];
    function odd(element, index, array) {
        return (element%2);
    }
    console.log(arr.filter(odd));
    >>>[13, 1, 5]

Array.every() 和 Array.some()

如果数组中的所有元素都通过由提供的函数实现的测试,Array.every() 将返回 true。如果数组中的任一元素都通过由提供的函数实现的测试,Array.some() 将返回 true

    var arr = [8, 10, 13, 10, 8, 1, 5];
    function odd(element, index, array) {
        return (element%2);
    }
    console.log(arr.every(odd));
    >>>false

    console.log([1,3,5].every(odd))
    >>>true

    console.log(arr.some(odd));
    >>>true

    console.log([2,4,6].some(odd))
    >>>false

Array.indexOf() 和 Array.lastIndexOf()

    var arr = [8, 10, 13, 10, 8, 1, 5];

    console.log("lastIndexOF is " + arr.lastIndexOf(10));
    >>>lastIndexOF is 3

    console.log("indexOF is " + arr.indexOf(10));
    >>>indexOF is 1

Others

JSON.parse()和JSON.stringify()

Date.now()

Function.prototype.bind()

link

posted @ 2015-11-27 10:31  fannet  阅读(215)  评论(0编辑  收藏  举报