简体   繁体   English

stackoverflow 代码段与浏览器控制台中的 console.log() 不同 - 为什么?

[英]Different console.log()s in stackoverflow snippet vs. browser console - why?

 const func = function() { this.name = 'mon' } let f1 = new func let f2 = Object.create(f1) let f3 = Object.create(f2) // The following comments are what the browser console logs: console.log(f1.__proto__) // {constructor: f} console.log(f2.__proto__) // func {name: "mon"} console.log(f3.__proto__) // func {} (here's where this snippet logs func {"name": "mon"})

Also, is an object's 'type' in JS determined by its 'nearest' constructor?另外,JS 中对象的“类型”是否由其“最近的”构造函数决定? (ie func being the type the browser logs for f1 & f2 )? (即 func 是浏览器为f1f2记录的类型)?

The difference is that the Stack Snippet console (whose code can be found here ) iterates over properties using for..in .不同之处在于 Stack Snippet 控制台(可在此处找到其代码)使用for..in迭代属性。 This will include enumerable properties anywhere on the prototype chain.将包括原型链上任何地方的可枚举属性。 This is (part of) how the snippet console figures out properties to log:这是(部分)代码段控制台如何计算要记录的属性:

    function n(e) {
        var n = [];
        for (var o in e)
            n.push(o);
        return n
    }

For example:例如:

 const proto = { prop: 'val' }; const obj = Object.create(proto); console.log(obj); // browser console shows `Object`, but no `prop` property

In contrast, in the browser console, only properties directly on a logged object will be displayed.相比之下,在浏览器控制台中,只会显示直接在记录对象上的属性。 In order to access the internal prototype of the logged object (and see possible properties on the prototype), you'll have to click on the __proto__ property to expand it.为了访问记录对象的内部原型(并查看原型上可能的属性),您必须单击__proto__属性以展开它。

在此处输入图片说明

Because the constructor property of a someFunction.prototype is not enumerable, it won't be iterated over via for..in , so it's seen in the browser console, but not in the snippet console.因为someFunction.prototypeconstructor属性是不可枚举的,它不会通过for..in迭代,所以它可以在浏览器控制台中看到,但在代码段控制台中看不到。

 const func = function() { this.name = 'mon' } console.log(func.prototype.hasOwnProperty('constructor')); for (const prop in func.prototype) { console.log(prop); // Nothing is logged }

Also, is an object's 'type' in JS determined by its 'nearest' constructor?另外,JS 中对象的“类型”是否由其“最近的”构造函数决定? (ie func being the type the browser logs for f1 & f2 )? (即 func 是浏览器为 f1 和 f2 记录的类型)?

Every object has exactly one internal prototype.每个对象都只有一个内部原型。 In your snippet, the internal prototype of f3 is f2 , the internal prototype of f2 is f1 , and the internal prototype of f1 is func.prototype .在你的代码段,内部原型f3f2 ,内部原型f2f1 ,并且内部原型f1func.prototype Something in a prototype chain may also have a constructor property ( func.prototype.constructor === func ), but the fact that such a property exists doesn't really have an effect on the internal prototype chain, it's just an convenient method to link a .prototype object to its associated function .原型链中的某些东西也可能有一个constructor属性( func.prototype.constructor === func ),但是这样一个属性存在的事实对内部原型链没有真正的影响,它只是一个方便的方法将.prototype对象链接到其关联的function

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

相关问题 浏览器 console.log 和代码 console.log 显示不同 - Browser console.log and code console.log showing different Javascript / jQuery:为什么使用<a>标记Vs</a>对event.target使用不同的结果<a>。</a> <a>console.log中的其他元素,请注意$(this)的alert()</a> - Javascript/jQuery: Why different result for event.target using <a> tag Vs. other Elements in console.log, alert() with $(this) in mind VS 代码片段到当前行下方的 console.log 选择 - VS Code snippet to console.log selection beneath current line 函数 - 显示与执行 console.log - Functions - Display vs. Execution console.log Javascript console.log(object) 与连接字符串 - Javascript console.log(object) vs. concatenating string JS 中的 console.log 与 return 的问题 - Issues with console.log vs. return in JS 为什么我的函数返回与Chrome浏览器中的console.log()不同 - Why is my function return different than my console.log() in Chrome browser 为什么刷新浏览器前后console.log的结果会不一样? - Why the result of console.log will be different before and after I refresh the browser? 为什么console.log在分配之前会记录一个值? - Why does console.log log a value before it's assigned? console.log与return:不同的结果(JS) - console.log vs return: different results (JS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM