• JavaScript Good Parts学习笔记-对象篇


    1
    检索一个对象的值,可以用[] 括住一个字符串表达式(是一个合法的标识符而且不是保留字),也可以用.(点)
    推荐用点,因为这样更紧凑而且可读性好。
    如果检索一个不存在的属性,将返回undefined。
    2
    Attempting to retrieve values from undefined will throw a TypeError exception. This
    can be guarded against with the && operator:
    flight.equipment // undefined
    flight.equipment.model // throw "TypeError"
    flight.equipment && flight.equipment.model // undefined
    3
    对象通过引用来传递,他们永远不会被复制。
    Objects are passed around by reference. They are never copied:


    4
    原型(prototype) 不是很懂,把原版的英文copy下来。

    Every object is linked to a prototype object from which it can inherit properties. All
    objects created from object literals are linked to Object.prototype, an object that
    comes standard with JavaScript.
    When youmake a new object, youcan select the object that should be its prototype.
    The mechanism that JavaScript provides to do this is messy and complex, but it can
    be significantly simplified. We will add a create method to the Object function. The
    create method creates a new object that uses an old object as its prototype. There
    will be much more about functions in the next chapter.
    if (typeof Object.create !== 'function') {
        Object.create = function(o) {
            var F = function() {};
            F.prototype = o;
            return new F();
        };
    }
    var another_stooge = Object.create(stooge);

    The prototype link has no effect on updating. When we make changes to an object,
    the object’s prototype is not touched:
    another_stooge['first-name'] = 'Harry';
    another_stooge['middle-name'] = 'Moses';
    another_stooge.nickname = 'Moe';
    The prototype link is used only in retrieval. If we try to retrieve a property value from
    an object, and if the object lacks the property name, then JavaScript attempts to
    retrieve the property value from the prototype object. And if that object is lacking the
    property, then it goes to its prototype, and so on until the process finally bottoms out
    with Object.prototype. If the desired property exists nowhere in the prototype chain,
    then the result is the undefined value. This is called delegation.
    The prototype relationship is a dynamic relationship. If we add a new property to a
    prototype, that property will immediately be visible in all of the objects that are
    based on that prototype:
    stooge.profession = 'actor';
    another_stooge.profession // 'actor'
    We will see more about the prototype chain in Chapter 6.

    5
    检查对象的属性,(不包括函数和原型链上的属性)
    可以使用hasOwnProperty函数
    The other approach is to use the hasOwnProperty method, which returns true if the
    object has a particular property. The hasOwnProperty method does not look at the
    prototype chain:
    flight.hasOwnProperty('number') // true
    flight.hasOwnProperty('constructor') // false

    6
    delete可以删除对象的属性
    another_stooge.nickname // 'Moe'
    // Remove nickname from another_stooge, revealing
    // the nickname of the prototype.
    delete another_stooge.nickname;
    another_stooge.nickname // 'Curly'

  • 相关阅读:
    URL中#号(井号)的作用
    Sublime Text2 快捷键汇总
    Sublime Text2 使用及插件配置
    Sumblime Text 2 常用插件以及安装方法
    CSS禁止选择文本功能(兼容IE,火狐等浏览器)
    JavaScript 判断 URL
    纯真IP数据库格式读取方法(JAVA/PHP/Python)
    Sublime Text 2 性感无比的代码编辑器!程序员必备神器!跨平台支持Win/Mac/Linux
    base64:URL背景图片与web页面性能优化
    数独DFS实现
  • 原文地址:https://www.cnblogs.com/sdfczyx/p/6396594.html
Copyright © 2020-2023  润新知