<><br>>Chapter 3 Object s 2016.12.24
5890 ワード
The simple types of JavaScript are numbers,stings,boleans,null,and undefined.All other values.Numbers,stins,and boleans are oooject-likeinaththetheeeeeeeeeeeeeeeeeeeebubububububurerererererereeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeemmmmblblble e e e e e e e e e e e e e e e e e e e e e e e、arrays are object、functions are object、reglar expressions are object、and、of course、object are object.JSのシンプルなタイプは数字、文字列、ブール型、nullとundefinedがあり、その他の値はすべて対象です.数字、文字列、ブール型はオブジェクトと似ているところがありますが、数字、文字列、ブール型は可変ではなく、オブジェクトは可変的なkeyの集合です.配列はオブジェクト、関数はオブジェクト、正規表現はオブジェクトです.
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 for undefined.は、いくつかの属性といくつかの属性からなります.属性名は任意の文字列でもいいし、空の文字列でもあります.属性値は、undefinedを除く任意のJS値とすることができる.
Objecs in JavaScript ares.Object-free.The isのconstrant on the names of new properties or on the values of properties.Object s areful for collecting and organizing data.Object can contaties can cant the ojectiesオブジェクトを含むことができます.したがって、オブジェクトは木や図の結果を容易に示すことができる.
JavaScript includes a prototype linkage feature that allows one object to inheit the properties of another.When used well,this can reduce oject initialization time and memory consumption.JSはプロトタイプリンクの特性を含み、オブジェクトの別の属性を継承することができます.この特性をうまく利用すれば、対象の初期化時間とメモリ占有量を減らすことができる.
Object Literals菗
Objecs aresed around by reference.They are never copied:
Every oject is linked to a prototype object from which it can inhers properties.All object created from object literals ares to Object.prototype,an oject that compand with JavaScrript.すべてのojectでオブジェクトを作成します.When you make a new object,you can select the object ththat Shaat shout t t t prototype.The mechaism that JavaScrpt provides to to this is messy andcoplex,but it it it can be signifintlymplified simplified.Weadeefied.Weaaaaatttttfffffmimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimis prototype.
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 for undefined.は、いくつかの属性といくつかの属性からなります.属性名は任意の文字列でもいいし、空の文字列でもあります.属性値は、undefinedを除く任意のJS値とすることができる.
Objecs in JavaScript ares.Object-free.The isのconstrant on the names of new properties or on the values of properties.Object s areful for collecting and organizing data.Object can contaties can cant the ojectiesオブジェクトを含むことができます.したがって、オブジェクトは木や図の結果を容易に示すことができる.
JavaScript includes a prototype linkage feature that allows one object to inheit the properties of another.When used well,this can reduce oject initialization time and memory consumption.JSはプロトタイプリンクの特性を含み、オブジェクトの別の属性を継承することができます.この特性をうまく利用すれば、対象の初期化時間とメモリ占有量を減らすことができる.
Object Literals菗
var empty_object = {};
var stooge = {
"first-name": "Jerome",
"last-name": "Howard"
};
var flight = {
airline: "Oceanic",
number: 815,
departure: {
IATA: "SYD",
time: "2004-09-22 14:55",
city: "Sydney"
},
arrival: {
IATA: "LAX",
time: "2004-09-23 10:42",
city: "Los Angeles"
}
};
Retrieval钾stooge["first-name"] // "Jerome"
flight.departure.IATA // "SYD"
stooge["middle-name"] // undefined
flight.status // undefined
stooge["FIRST-NAME"] // undefined
var middle = stooge["middle-name"] || "(none)";
var status = flight.status || "unknown";
flight.equipment // undefined
flight.equipment.model // throw "TypeError"
flight.equipment && flight.equipment.model // undefined
Update菵stooge['first-name'] = 'Jerome';
stooge['middle-name'] = 'Lester';
stooge.nickname = 'Curly';
flight.equipment = {
model: 'Boeing 777'
};
flight.status = 'overdue';
Reference钾Objecs aresed 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菵Every oject is linked to a prototype object from which it can inhers properties.All object created from object literals ares to Object.prototype,an oject that compand with JavaScrript.すべてのojectでオブジェクトを作成します.When you make a new object,you can select the object ththat Shaat shout t t t prototype.The mechaism that JavaScrpt provides to to this is messy andcoplex,but it it it can be signifintlymplified simplified.Weadeefied.Weaaaaatttttfffffmimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimimis prototype.
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 udating.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 oject,and if the object lacks the property name,then JavaScript apt torieve the property propertyand so until the process finally bottoms out with Object.prototype.If the desired property exists nowhere in the prototype chain,then the reult ist is the undefined value.This is caled degation.The protomicreditype protonitypethat property will immediately be visible in the object that are based on that prototype:stooge.profession = 'actor';
another_stooge.profession // 'actor'
Reflection舸typeof flight.number // 'number'
typeof flight.status // 'string'
typeof flight.arrival // 'object'
typeof flight.manifest // 'undefined'
typeof flight.toString // 'function'
typeof flight.constructor // 'function'
flight.hasOwnProperty('number') // true
flight.hasOwnProperty('constructor') // false
Enumeration〓〓〓〓var name;
for (name in another_stooge) {
if (typeof another_stooge[name] !== 'function') {
document.writeln(name + ': ' + another_stooge[name]);
}
}
var i;
var properties = ['first-name', 'middle-name', 'last-name', 'profession'];
for (i = 0; i < properties.length; i += 1) {
document.writeln(properties[i] + ': ' + another_stooge[properties[i]]);
}
Delete钾another_stooge.nickname // 'Moe'
// Remove nickname from another_stooge, revealing the nickname of the prototype.
delete another_stooge.nickname;
another_stooge.nickname // 'Curly'
Global Abatement