• JS Objects


    The simple types of JavaScript are numbers, strings, booleans (true and false), null, and undefined. All other values are objects. Numbers, strings, and booleans are object-like in that they have methods, but they are immutable. Objects in JavaScript  are mutable keyed collections. In JavaScript, arrays are objects, functions are objects,regular expressions are objects, and, of course, objects are objects.

    An object is a container of properties, where a property has a name and a value. A
    property name can be any string, including the empty string. A property value can be any JavaScript value except for undefined.
    Objects in JavaScript are class-free. There is no constraint on the names of new properties or on the values of properties. Objects are useful for collecting and organizing data. Objects can contain other objects, so they can easily represent tree or graph  structures.
    JavaScript includes a prototype linkage feature that allows one object to inherit the properties of another. When used well, this can reduce object initialization time and memory consumption.

    js中的对象是无类别的(class-free)。它对新属性的名字和值没有约束。对象适合用于收集和管理数据。对象可以包含其他对象。所以他们可以容易地表示成树形或图形结构。
    js包括一个原型链特性,运行对象继承另一对象的属性。正确地使用它能减少对象初始化的时间和内存消耗。

    对象字面量

    对象字面量提供了一种可以非常方便地创建新对象值的表示法。一个object literal
    就是包围在一对花括号中的零或多个“名/值”对。对象字面量可以出现在任何允许表示式出现的地方。

    Object Literals
    Object literals provide a very convenient notation for creating new object values. An object literal is a pair of curly braces surrounding zero or more name/valuepairs. An object literal can appear anywhere an expression can appear:
    var empty_object = {};
    var stooge = {
    "first-name": "Jerome",
    "last-name": "Howard"
    };

    Retrieval
    Values can be retrieved from an object by wrapping a string expression in a [ ] suffix.If the string expression is a string literal, and if it is a legal JavaScript name and not a reserved word, then the . notation can be used instead. The . notation is preferred because it is more compact and it reads better:

    stooge["first-name"] // "Jerome"
    flight.departure.IATA // "SYD"
    The undefined value is produced if an attempt is made to retrieve a nonexistentmember:
    stooge["middle-name"] // undefined
    flight.status // undefined
    stooge["FIRST-NAME"] // undefined
    The || operator can be used to fill in default values:
    var middle = stooge["middle-name"] || "(none)";
    var status = flight.status || "unknown";
    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

    Update
    A value in an object can be updated by assignment. If the property name already exists in the object, the property value is replaced:
    stooge['first-name'] = 'Jerome';
    If the object does not already have that property name, the object is augmented:(扩充)
    stooge['middle-name'] = 'Lester';
    stooge.nickname = 'Curly';
    flight.equipment = {
    model: 'Boeing 777'
    };
    flight.status = 'overdue';

    Reference
    Objects are passed around by reference. They are never copied:

    对象通过引用来传递。他们永远不会被拷贝。

    var x = stooge;
    x.nickname = 'Curly';
    var nick = stooge.nickname;
         // nick is 'Curly' because x and stooge   are references to the same object
    var a = {}, b = {}, c = {};
    // a, b, and c each refer to a different empty object
    a = b = c = {};
    // a, b, and c all refer to  the same empty object

    原型Prototype
    每个对象都连接到一个原型对象,并且他可以从中继承属性。所以通过字面量创建的对象都链接到object.prototpe这个js中标准的对象。
    当你创建一个新对象时,你可以选择某个对象作为他的原型。js提供的实现机制杂乱而复杂,但其实他可以被明显地简化。我们将给
    object增加一个beget方法,这个方法创建一个使用原对象作为其原型的新对象。

    删除对象某些属性

    The delete operator can be used to remove a property from an object. It will remove
    a property from the object if it has one. It will not touch any of the objects in the prototype
    linkage.

  • 相关阅读:
    html5+css3实现上拉和下拉刷新
    js求时间差
    screenX clientX pageX的区别
    HTML5实战与剖析之触摸事件(touchstart、touchmove和touchend)
    国内代码托管git-osc基础使用教程
    c# 实现获取汉字十六进制Unicode编码字符串
    C# 判断字符编码的六种方法
    UNICODE 区域对照表
    viewport
    Wingdings 2 符号编码对照表
  • 原文地址:https://www.cnblogs.com/youxin/p/2625003.html
Copyright © 2020-2023  润新知