摘要:一規(guī)范中二規(guī)范中三規(guī)范中四分析過程分析五證明流程
Why in JavaScript both "Object instanceof Function" and "Function instanceof Object" return true? 一、ECMA5.1規(guī)范中instanceof
/* how instanceof is defined by ECMA 5.1 Specification: The production RelationalExpression: RelationalExpression instanceof ShiftExpression is evaluated as follows: Let lref be the result of evaluating RelationalExpression. Let lval be GetValue(lref). Let rref be the result of evaluating ShiftExpression. Let rval be GetValue(rref). If Type(rval) is not Object, throw a TypeError exception. If rval does not have a [[HasInstance]] internal method, throw a TypeError exception. Return the result of calling the [[HasInstance]] internal method of rval with argument lval. ------->Not all objects will have [[HasInstance]] internal method, but functions. console.log(Object instanceof {}); TypeError: Expecting a function in instanceof check, but got二、ECMA5.1規(guī)范中[[HasInstance]]
/* how [[HasInstance]] has been defined in the ECMA 5.1 specification: Assume F is a Function object. When the [[HasInstance]] internal method of F is called with value V, the following steps are taken: If V is not an object, return false. Let O be the result of calling the [[Get]] internal method of F with property name "prototype". If Type(O) is not Object, throw a TypeError exception. Repeat Let V be the value of the [[Prototype]] internal property of V. If V is null, return false. If O and V refer to the same object, return true. ------->Take the prototype property of F and compare it with the [[Prototype]] internal property of O until it becomes null or prototype of F is the same as O. */三、ECMA5.1規(guī)范中[[prototype]]
/* what is the [[prototype]] internal property: All objects have an internal property called [[Prototype]]. The value of this property is either null or an object and is used for implementing inheritance. Whether or not a native object can have a host object as its [[Prototype]] depends on the implementation. Every [[Prototype]] chain must have finite length(that is, starting from any object, recursively accessing the [[Prototype]] internal property must eventually lead to a null value). ------->We can get this internal property with the Object.getPrototypeOf function */
/* [[HasInstance]] also talks about another property called prototype, which is specific to the Function objects. The value of the prototype property is used to initialise the [[Prototype]] internal property of a newly created object before the Function object is invoked as a constructor for that newly created object. ------->when a function object is used as a constructor, a new object will be created and the new object will have its internal [[Prototype]] initialized with this prototype property ------->function Test() {} Test.prototype.print = console.log; console.log(Object.getPrototypeOf(new Test()) === Test.prototype); # true */ console.log("-------------------------------------------------------" + " ");四、分析過程
//分析: console.log(Object instanceof Function); // true // It will fetch Function.prototype first and it will try // and find if that object is in the prototype hierarchy of Object. Let us see how that turns out console.log(Function.prototype); // [Function: Empty] console.log(Object.getPrototypeOf(Object)); // [Function: Empty] console.log(Object.getPrototypeOf(Object) === Function.prototype); // true //Since the Function.prototype matches the Object"s internal property [[Prototype]], it returns true console.log(Function instanceof Object); // true console.log(Object.prototype); // {} console.log(Object.getPrototypeOf(Function)); // [Function: Empty] console.log(Object.getPrototypeOf(Function) === Object.prototype); // false console.log(Object.getPrototypeOf(Object.getPrototypeOf(Function))); // {} Object.getPrototypeOf(Object.getPrototypeOf(Function)) === Object.prototype // true //Here, first we get the Object.prototype, which is {}. // Now it is trying to find if the same object {} is there in the Function"s prototype chain. // Immediate parent of Function is and Empty function. console.log(Object.getPrototypeOf(Function)); // [Function: Empty] //It is not the same as Object.prototype console.log(Object.getPrototypeOf(Function) === Object.prototype); // false //But the [[HasInstance]] algorithm doesn"t stop there. It repeats and gets up one more level console.log(Object.getPrototypeOf(Object.getPrototypeOf(Function))); // {} //And this is the same as Object.prototype. That is why this returns true. console.log("-------------------------------------------------------" + " ");五、證明流程
console.log(Object instanceof Function); // true console.log(Function instanceof Object); // true console.log(Object.prototype); // {} console.log(Object.getPrototypeOf(Function)); // [Function: Empty] console.log(Object.getPrototypeOf(Function) === Object.prototype); // false console.log(Object.getPrototypeOf(Object.getPrototypeOf(Function))); //{} console.log(Object.getPrototypeOf(Object.getPrototypeOf(Function)) === Object.prototype); // true console.log(Object.getPrototypeOf(Function)); //[Function: Empty] console.log(Object.getPrototypeOf(Function) === Object.prototype); // false console.log(Object.getPrototypeOf(Object.getPrototypeOf(Function))); // {} console.log("-------------------------------------------------------" + " ");
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/101182.html
摘要:另一方面,我不建議初次接觸的開發(fā)人員閱讀規(guī)范。在維護(hù)語言的最新規(guī)范。在這一點(diǎn)上,我想指出的是,絕對沒有人從上到下閱讀規(guī)范。拓展閱讀由于的定義,中的細(xì)節(jié)如冒泡錯誤,直到塊在規(guī)范中不存在。換句話說,會轉(zhuǎn)發(fā)中拋出的錯誤,并終止其余的步驟。 翻譯自:How to Read the ECMAScript Specification Ecmascript 語言規(guī)范 The ECMAScr...
摘要:,微軟發(fā)布,同時發(fā)布了,該語言模仿同年發(fā)布的。,公司在瀏覽器對抗中沒落,將提交給國際標(biāo)準(zhǔn)化組織,希望能夠成為國際標(biāo)準(zhǔn),以此抵抗微軟。同時將標(biāo)準(zhǔn)的設(shè)想定名為和兩類。,尤雨溪發(fā)布項(xiàng)目。,正式發(fā)布,并且更名為。,發(fā)布,模塊系統(tǒng)得到廣泛的使用。 前言 作為程序員,技術(shù)的落實(shí)與鞏固是必要的,因此想到寫個系列,名為 why what or how 每篇文章試圖解釋清楚一個問題。 這次的 why w...
摘要:不包括作為其嵌套函數(shù)的被解析的源代碼。作用域鏈當(dāng)代碼在一個環(huán)境中執(zhí)行時,會創(chuàng)建變量對象的一個作用域鏈。棧結(jié)構(gòu)最頂層的執(zhí)行環(huán)境稱為當(dāng)前運(yùn)行的執(zhí)行環(huán)境,最底層是全局執(zhí)行環(huán)境。無限制函數(shù)上下文?;蛘邟伋霎惓M顺鲆粋€執(zhí)行環(huán)境。 前言 其實(shí)規(guī)范這東西不是給人看的,它更多的是給語言實(shí)現(xiàn)者提供參考。但是當(dāng)碰到問題找不到答案時,規(guī)范往往能提供想要的答案 。偶爾讀一下能夠帶來很大的啟發(fā)和思考,如果只讀一...
摘要:本文總結(jié)了的各種情況,并從規(guī)范的角度探討了的具體實(shí)現(xiàn),希望對大家理解有所幫助。規(guī)范規(guī)范里面詳細(xì)介紹了的實(shí)現(xiàn)細(xì)節(jié),通過閱讀規(guī)范,我們可以更準(zhǔn)確的理解上述四種情況到底是怎么回事。由于本人能力有限,如有理解錯誤的地方還望指出。 this是面向?qū)ο缶幊讨械囊粋€概念,它一般指向當(dāng)前方法調(diào)用所在的對象,這一點(diǎn)在java、c++這類比較嚴(yán)格的面向?qū)ο缶幊陶Z言里是非常明確的。但是在javascript...
摘要:在上百種語言中算是命好的一個,還有就是最近納入高考體系的。由以下三個部分構(gòu)成。就是對實(shí)現(xiàn)該標(biāo)準(zhǔn)規(guī)定的各個方面內(nèi)容的語言的描述。是針對但經(jīng)過擴(kuò)展的用于的應(yīng)用程序編程接口。將頁面映射為由節(jié)點(diǎn)構(gòu)成的樹狀結(jié)構(gòu)。 JavaScript的歷史這里就不再贅述了,當(dāng)然JavaScript的歷史還是比較有意思的。在上百種語言中JavaScript算是‘命’好的一個,還有就是最近納入高考體系的python...
閱讀 3698·2021-11-22 15:24
閱讀 1607·2021-09-26 09:46
閱讀 1919·2021-09-14 18:01
閱讀 2614·2019-08-30 15:45
閱讀 3533·2019-08-30 14:23
閱讀 1881·2019-08-30 12:43
閱讀 2920·2019-08-30 10:56
閱讀 805·2019-08-29 12:20