资讯专栏INFORMATION COLUMN

Node.js中Object与Function在ECMA 规范中的关系

LdhAndroid / 2002人阅读

摘要:一规范中二规范中三规范中四分析过程分析五证明流程

Why in JavaScript both "Object instanceof Function" and "Function instanceof Object" return true? 一、ECMA5.1规范中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规范中[[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规范中[[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("-------------------------------------------------------" + "

");

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/101182.html

相关文章

  • 怎样阅读 ECMAScript 规范

    摘要:另一方面,我不建议初次接触的开发人员阅读规范。在维护语言的最新规范。在这一点上,我想指出的是,绝对没有人从上到下阅读规范。拓展阅读由于的定义,中的细节如冒泡错误,直到块在规范中不存在。换句话说,会转发中抛出的错误,并终止其余的步骤。 翻译自:How to Read the ECMAScript Specification Ecmascript 语言规范 The ECMAScr...

    lpjustdoit 评论0 收藏0
  • 什么是 JAVASCRIPT?

    摘要:,微软发布,同时发布了,该语言模仿同年发布的。,公司在浏览器对抗中没落,将提交给国际标准化组织,希望能够成为国际标准,以此抵抗微软。同时将标准的设想定名为和两类。,尤雨溪发布项目。,正式发布,并且更名为。,发布,模块系统得到广泛的使用。 前言 作为程序员,技术的落实与巩固是必要的,因此想到写个系列,名为 why what or how 每篇文章试图解释清楚一个问题。 这次的 why w...

    ephererid 评论0 收藏0
  • ECMA 规范解读 Javascript 可执行上下文概念

    摘要:不包括作为其嵌套函数的被解析的源代码。作用域链当代码在一个环境中执行时,会创建变量对象的一个作用域链。栈结构最顶层的执行环境称为当前运行的执行环境,最底层是全局执行环境。无限制函数上下文。或者抛出异常退出一个执行环境。 前言 其实规范这东西不是给人看的,它更多的是给语言实现者提供参考。但是当碰到问题找不到答案时,规范往往能提供想要的答案 。偶尔读一下能够带来很大的启发和思考,如果只读一...

    daryl 评论0 收藏0
  • Ecma规范深入理解this

    摘要:本文总结了的各种情况,并从规范的角度探讨了的具体实现,希望对大家理解有所帮助。规范规范里面详细介绍了的实现细节,通过阅读规范,我们可以更准确的理解上述四种情况到底是怎么回事。由于本人能力有限,如有理解错误的地方还望指出。 this是面向对象编程中的一个概念,它一般指向当前方法调用所在的对象,这一点在java、c++这类比较严格的面向对象编程语言里是非常明确的。但是在javascript...

    rottengeek 评论0 收藏0
  • JavaScript高级程序设计学习笔记一(JavaScript简介)

    摘要:在上百种语言中算是命好的一个,还有就是最近纳入高考体系的。由以下三个部分构成。就是对实现该标准规定的各个方面内容的语言的描述。是针对但经过扩展的用于的应用程序编程接口。将页面映射为由节点构成的树状结构。 JavaScript的历史这里就不再赘述了,当然JavaScript的历史还是比较有意思的。在上百种语言中JavaScript算是‘命’好的一个,还有就是最近纳入高考体系的python...

    supernavy 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<