简体   繁体   English

为什么刷新浏览器前后console.log的结果会不一样?

[英]Why the result of console.log will be different before and after I refresh the browser?

I have the code snippet like this:我有这样的代码片段:

 function Person(myName,myAge) { this.name = myName this.age = myAge } Person.prototype = { say:function() { console.log("Hi"); } } let person = new Person("John",12); console.log(Person.prototype) console.log(Person.prototype.constructor) console.log(person.__proto__)

I can't understand why when I first execute the code(by right click and open the file in chrome),the devtool will log:我不明白为什么当我第一次执行代码时(通过右键单击并在 chrome 中打开文件),devtool 会记录:

Object
ƒ Object() { [native code] }
Object

But when I refresh the browser,the devtool will log:但是当我刷新浏览器时,开发工具会记录:

{say: ƒ}
ƒ Object() { [native code] }
{say: ƒ}

So, in the first log,it actually didn't log the complete object,right?It just log Object in order to tell you that it is an object instead of telling you the content of this object.Why?What does the browser do when I refresh it? So, in the first log,it actually didn't log the complete object,right?It just log Object in order to tell you that it is an object instead of telling you the content of this object.Why?What does the browser do当我刷新它?

Changing the prototype directly is not a good idea.直接更改原型不是一个好主意。 No matter if you use setPrototypeOf or directly .prototype .无论您是使用setPrototypeOf还是直接使用.prototype

From MDN来自MDN

Warning : Changing the [[Prototype]] of an object is, by the nature of how modern JavaScript engines optimize property accesses, currently a very slow operation in every browser and JavaScript engine .警告:根据现代 JavaScript 引擎如何优化属性访问的性质,更改 object 的 [[Prototype]],目前在每个浏览器和 Z686155AF75A60A0F6E9D80C1F7EDD3 引擎中的操作都非常缓慢 In addition, the effects of altering inheritance are subtle and far-flung, and are not limited to simply the time spent in the Object.setPrototypeOf(...) statement, but may extend to any code that has access to any object whose [[Prototype]] has been altered.此外,更改 inheritance 的影响是微妙而广泛的,并且不仅限于在 Object.setPrototypeOf(...) 语句中花费的时间,而是可以扩展到可以访问任何 ZA8CFDE6331AC49EB66 的任何代码[原型]]已被更改。

Because this feature is a part of the language, it is still the burden on engine developers to implement that feature performantly (ideally).由于此功能是语言的一部分,因此引擎开发人员仍然需要高效地(理想地)实现该功能 Until engine developers address this issue, if you are concerned about performance, you should avoid setting the [[Prototype]] of an object.在引擎开发人员解决此问题之前,如果您担心性能,则应避免设置 object 的 [[Prototype]]。 Instead, create a new object with the desired [[Prototype]] using Object.create().相反,使用 Object.create() 创建具有所需 [[Prototype]] 的新 object。

You should use Object.create instead with the desired prototype.您应该使用Object.create来代替所需的原型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM