Can I say ConstructorName.prototype={}?

Asked 2 years ago, Updated 2 years ago, 17 views

The following is written on the page below, but how do you treat .prototype={} in general?
·According to the specifications, it is permitted but should not be used
·Never use
·According to the rules
·You can use it separately

Can I say ClassName.prototype={}?

http://qiita.com/LightSpeedC/items/d307d809ecf2710bd957

Is the description on the page below just to check the behavior of instanceof?

C.prototype={};

https://developer.mozilla.org/ja/docs/JavaScript/Reference/Operators/instanceof

Question
·I understand what you are saying on the first page, but why is this writing allowed in the first place?
·Should the rules restrict the use of Object.create() only?
·What did you do when there was no Object.create() in ES3?
·If .prototype={} is enabled, it may return unintended results for instanceof…
·Is the idea that instanceof cannot be used together with .prototype={} correct?

javascript

2022-09-29 22:11

1 Answers

In conclusion, if you know about .prototype.constructor and instanceof, you can do it.
In the first place, it is rare to write instanceof to a class that I made myself.
If you really need instanceof to work as usual,

C.prototype={
  constructor: C, // ← Insert this
  ......
  ......
}

That's all you have to do.

The article is mainly a memorandum of understanding for the stage of switching from ES3 to ES5, and in the end, it is intended to effectively use Object.create, which comes from ES5.

"However, now we are switching from ES5 to ES6, so ""best"" has changed."
I have a lot to say about this area, but
To put it simply, "Please understand the meaning and use it effectively."

Unlike when the article was first published, "__proto__" has already been decided to be standard, so you can use it a lot understanding that the support environment is limited.


2022-09-29 22:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.