Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基元上的isPrototypeOf()函数_Javascript_Prototype_Prototype Chain - Fatal编程技术网

Javascript 基元上的isPrototypeOf()函数

Javascript 基元上的isPrototypeOf()函数,javascript,prototype,prototype-chain,Javascript,Prototype,Prototype Chain,声明 isPrototypeOf()方法检查一个对象是否存在于另一个对象的原型链中 当我创建一个基本变量时,比如 var a=0 并检查其[[Prototype]](而不是a.Prototype) console.log(a.\uuuuu proto\uuuuu)//在chrome上 它打印数字{0,可由2:ƒ、构造函数:ƒ、toExponential:ƒ、toFixed:ƒ、}。显然,这个值似乎是Number.prototype中的值 所以,我期望Number.prototype.isProt

声明

isPrototypeOf()方法检查一个对象是否存在于另一个对象的原型链中

当我创建一个基本变量时,比如

var a=0

并检查其[[Prototype]](而不是
a.Prototype

console.log(a.\uuuuu proto\uuuuu)//在chrome上

它打印
数字{0,可由2:ƒ、构造函数:ƒ、toExponential:ƒ、toFixed:ƒ、}
。显然,这个值似乎是
Number.prototype
中的值

所以,我期望
Number.prototype.isPrototypeOf(a)
返回true,因为
Number.prototype
确实存在于
a的prototype链(prototype链)中

但是相反,
Number.prototype.isPrototypeOf(a)的输出为false

var a=0;
console.log(a.uuu proto_uuu);
控制台。日志(编号。原型);
console.log(Number.prototype==a.\uuuu proto\uuuuu);

log(Number.prototype.isPrototypeOf(a))当您访问原语上的属性或调用方法时,JavaScript实际上正在从原语创建一个新的Number对象,之后新对象会立即被丢弃。这就是为什么您可以访问
\uuu proto\uu
属性。但是,原语不是数字对象。

正如您在规范中看到的那样,
isProtoTypeOf
的第一条规则是“如果类型(V)不是对象,则返回false”(其中V是传递给方法的值)。

尝试使用
a=newnumber(0)
<代码>编号
和编号
不一样。我想这就是问题所在。像你说的那样,用原语可能不起作用。啊,现在有道理了。我不知道“包装器”的概念,谢谢你的帮助@KentaNomoto没问题。哦,我从来没有检查过这个网站,但现在这更有意义了,在接收原始LHS时立即返回
false
听起来确实更有效。谢谢你的帮助!