简体   繁体   English

JavaScript:为什么在所有者搜索代码中我得到的是假而不是真?

[英]JavaScript: why I get false instead of true in my code of property owner searching?

I learn JavaScript and play with it. 我学习JavaScript并使用它。 Why I get false instead of true in my code of property owner searching? 为什么在我的财产所有者搜索代码中我得到的是false而不是true What is my mistake? 我怎么了

/* Get the object containing the property. */
Object.prototype.where = function (propName){ return this.hasOwnProperty(propName) ? this :
    (this.constructor.prototype ? null : this.where.bind(this.constructor.prototype, propName)());};

let a = {foo : 123}, b = Object.create(a), c = a == b.where('foo');

process.stdout.write(c.toString()); // false

Because b.constructor.prototype != a . 因为b.constructor.prototype != a You will want to use Object.getPrototypeOf to follow the prototype chain. 您将要使用Object.getPrototypeOf跟随原型链。

a=Object.create(b);
//constructor: Object
//constructor.prototype: Object.prototype

//really inherits from: b

a = new b();
//constructor: b
//constructor.prototype: b.prototype

//really inherits from: b.prototype

Your code just works with constructor functions and the new operator, in all the other cases it goes wrong, as it does not inherit from the constructors prototype directly. 您的代码只适用于构造函数和new运算符,在所有其他情况下,它都会出错,因为它不会直接从构造函数原型继承。 It would be better to look up the prototype chain instead: 最好改为查找原型链:

Object.where = function (obj,propName){ 
  return obj.hasOwnProperty(propName) ? 
   obj :
(!Object.getPrototypeOf(obj) ? null : Object.where(Object.getPrototypeOf(obj), propName));
};

Object.where(Object.create({a:0}),"a");

Note that overriding Object.prototype is a bad idea, as it confuses all for loops. 请注意,重写Object.prototype是一个坏主意,因为它会使所有for循环感到困惑。 So one may set enumerable to false, or one simply make it static... 因此,可以将enumerable设置为false,或者只是将其设置为静态...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这段代码返回 true 而不是 false - why is this code returning true instead of false 为什么有时返回true而不是false导致我的代码无法正常工作? - Why my code doesn't work correctly by sometimes returning true instead of false? 为什么(!+ [] + [])是'true'而且(false + [])在Javascript中是'false'? - Why (!+[]+[]) is 'true' and (false + []) is 'false' in Javascript? 为什么我的原型内的javascript函数返回true或false - why my javascript function inside prototype return true or false 为什么 "true" == true 在 JavaScript 中显示 false? - Why does "true" == true show false in JavaScript? 为什么我在邮递员的架构中收到此错误:架构有效 | 断言错误:未知属性(不在架构中):预期假等于真 - Why am I getting this error in my schema in postman: Schema is valid | AssertionError: Unknown property (not in schema): expected false to equal true javascript hasOwnProperty返回true而不是false? - javascript hasOwnProperty returns true instead of false? 在JavaScript中返回假值,而不是true / false - Returning falsy values in javascript instead of true/false 为什么此函数返回true而不是false - Why does this function return true instead of false 如何从Android中的JavaScript获取对/错? - How can I get true/false from JavaScript in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM