object creation, null-prototype object

object:(string) key/value pair, with a private property which holds a link to another object called its?prototype.?

Objects can be created:

單個(gè)對(duì)象:

1.using the?Object()?constructor?

2.or the?object initializer / literal syntax.

3.Object.create(null)

批量生產(chǎn):

constructor function + prototype

object literal & new constructor

Objects are key/value pairs. The most common way to create an object is with curly braces?{}?and you add properties and methods to an object using dot notation.

let animal={}

animal.name='Leo'

animal.energy=10

animal.eat=function(amount){

????console.log(`${this.name} is eating.`)

????this.energy+=amount

}

animal.sleep=function(length){

????console.log(`${this.name} is sleeping.`)

????this.energy+=length

}

animal.play=function(length){

????console.log(`${this.name} is playing.`)

????this.energy-=length

}

Simple. Now odds are in our application we'll need to create more than one animal. Naturally the next step for this would be to encapsulate that logic inside of a function that we can invoke whenever we needed to create a new animal. We'll call this pattern?Functional Instantiation?and we'll call the function itself a "constructor function" since it's responsible for "constructing" a new object.


Nearly all?objects?in JavaScript are instances of?Object; a typical object inherits properties (including methods) from?Object.prototype, although these properties may be shadowed (a.k.a. overridden). The only objects that don't inherit from?Object.prototype?are those with?null?prototype, or descended from other?null?prototype objects.

Changes to the?Object.prototype?object are seen by?all?objects through prototype chaining, unless the properties and methods subject to those changes are overridden further along the prototype chain. This provides a very powerful although potentially dangerous mechanism to override or extend object behavior. To make it more secure,?Object.prototype?is the only object in the core JavaScript language that has?immutable prototype?— the prototype of?Object.prototype?is always?null?and not changeable.


The?__proto__?property is deprecated and should not be used. The?Object.getPrototypeOf()?and?Object.setPrototypeOf()?alternatives are static methods.

The?propertyIsEnumerable()?and?hasOwnProperty()?methods can be replaced with the?Object.getOwnPropertyDescriptor()?and?Object.hasOwn()?static methods, respectively.

The?isPrototypeOf()?method can usually be replaced with?instanceof, if you are checking the?prototype?property of a constructor.


null-prototype objects: The term "null-prototype object" often also includes any object without?Object.prototype?in its prototype chain. Such objects can be created with?extends null?when using classes.

Almost all objects in JavaScript ultimately inherit from?Object.prototype?(see?inheritance and the prototype chain). However, you may create?null-prototype objects using?Object.create(null)?or the?object initializer syntax?with?__proto__: null. You can also change the prototype of an existing object to?null?by calling?Object.setPrototypeOf(obj, null).

const obj = Object.create(null);

Object.setPrototypeOf(obj, null);

const obj2 = { __proto__: null };

An object with a?null?prototype can behave in unexpected ways, because it doesn't inherit any object methods from?Object.prototype. This is especially true when debugging, since common object-property converting/detecting utility functions may generate errors, or lose information (especially if using silent error-traps that ignore errors).

For example, the lack of?Object.prototype.toString()?often makes debugging intractable:

const normalObj = {}; // create a normal object

const nullProtoObj = Object.create(null); // create an object with "null" prototype

console.log(`normalObj is: ${normalObj}`); // shows "normalObj is: [object Object]"

console.log(`nullProtoObj is: ${nullProtoObj}`); // throws error: Cannot convert object to primitive value

alert(normalObj); // shows [object Object]

alert(nullProtoObj); // throws error: Cannot convert object to primitive value

In practice, objects with?null?prototype are usually used as a cheap substitute for?maps. The presence of?Object.prototype?properties will cause some bugs:

const ages = { alice: 18, bob: 27 };

function hasPerson(name) {

? return name in ages;

}

function getAge(name) {

? return ages[name];

}

hasPerson("hasOwnProperty"); // true

getAge("toString"); // [Function: toString]

Using a null-prototype object removes this hazard without introducing too much complexity to the?hasPerson?and?getAge?functions:

const ages = Object.create(null, {

? alice: { value: 18, enumerable: true },

? bob: { value: 27, enumerable: true },

});

hasPerson("hasOwnProperty"); // false

getAge("toString"); // undefined

對(duì)象子類型
函數(shù)的構(gòu)造調(diào)用constructor創(chuàng)建對(duì)象
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌暖哨,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件苫费,死亡現(xiàn)場(chǎng)離奇詭異被环,居然都是意外死亡楼雹,警方通過查閱死者的電腦和手機(jī)荐开,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門付翁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人晃听,你說我怎么就攤上這事百侧∨槭叮” “怎么了?”我有些...
    開封第一講書人閱讀 162,931評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵佣渴,是天一觀的道長(zhǎng)辫狼。 經(jīng)常有香客問我,道長(zhǎng)辛润,這世上最難降的妖魔是什么膨处? 我笑而不...
    開封第一講書人閱讀 58,218評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮砂竖,結(jié)果婚禮上灵迫,老公的妹妹穿的比我還像新娘。我一直安慰自己晦溪,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評(píng)論 6 388
  • 文/花漫 我一把揭開白布挣跋。 她就那樣靜靜地躺著三圆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪避咆。 梳的紋絲不亂的頭發(fā)上舟肉,一...
    開封第一講書人閱讀 51,198評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音查库,去河邊找鬼路媚。 笑死,一個(gè)胖子當(dāng)著我的面吹牛樊销,可吹牛的內(nèi)容都是我干的整慎。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼围苫,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼裤园!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起剂府,我...
    開封第一講書人閱讀 38,926評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤拧揽,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后腺占,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體淤袜,經(jīng)...
    沈念sama閱讀 45,341評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評(píng)論 2 333
  • 正文 我和宋清朗相戀三年衰伯,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了铡羡。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,731評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡意鲸,死狀恐怖蓖墅,靈堂內(nèi)的尸體忽然破棺而出库倘,到底是詐尸還是另有隱情,我是刑警寧澤论矾,帶...
    沈念sama閱讀 35,430評(píng)論 5 343
  • 正文 年R本政府宣布教翩,位于F島的核電站,受9級(jí)特大地震影響贪壳,放射性物質(zhì)發(fā)生泄漏饱亿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評(píng)論 3 326
  • 文/蒙蒙 一闰靴、第九天 我趴在偏房一處隱蔽的房頂上張望彪笼。 院中可真熱鬧,春花似錦蚂且、人聲如沸配猫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽泵肄。三九已至,卻和暖如春淑翼,著一層夾襖步出監(jiān)牢的瞬間腐巢,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評(píng)論 1 269
  • 我被黑心中介騙來泰國打工玄括, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留冯丙,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,743評(píng)論 2 368
  • 正文 我出身青樓遭京,卻偏偏與公主長(zhǎng)得像胃惜,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子哪雕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容