new x().constructor doesn't ACTUALLY exist?

thelittlegumnut

I've been experimenting with JavaScript's world-renowned prototypical inheritance implementation. So far, everything makes sense to me except for one thing...

function base() {
    this.a = "baseObj property";
}
base.thisIs = "base constructor";

function derived() {
    this.b = "derivedObj property";
}
derived.thisIs = "derived constructor";

var baseObj = new base();

So far so good. baseObj.a returns "baseObj property" and baseObj.constructor.thisIs returns "base constructor".

But things begin to confuse me when I actually make something inherit the base object's values.

derived.prototype = baseObj;
derivedObj = new derived();

What ends up happening, is derivedObj.a returns "baseObj property". Good. derivedObj.b returns "derivedObj property". Good again. But derivedObj.constructor.thisIs returns "base constructor"...

For this to be happening, the interpreter must be failing to find derivedObj.constructor in derivedObj. So what it does, is it follows derivedObj.__proto__ to search there instead. Because the new keyword sets derivedObj.__proto__ equal to derived.prototype, which we set earlier to equal baseObj, derivedObj.__proto__ ends up returning baseObj.

This would explain why derivedObj.constructor has been forgotten about. It's seemingly useless. Interpreters don't need to use it to obtain derived.prototype; they can just use derivedObj.__proto__. The reality however, is that it could have been useful to use derivedObj.constructor in order to obtain the value of derived.thisIs.

But even after all that. It doesn't explain why it hasn't been forgotten in baseObj. Why does a .constructor exist within baseObj but not derivedObj? They were initialized the exact same way. With the new keyword.

Oriol

By default, functions have a prototype property, which is the object from which the instances inherit when the function is used as a constructor. That prototype object has a constructor property, which points back to the function.

The problem with derived.prototype = baseObj is that you replace the entire prototype, so you lose the original derived.prototype.constructor, which returned derived.

A way to solve it would be reassigning that property:

derived.prototype = baseObj;
derived.prototype.constructor = derived;

However, that will alter the baseObj object. This is usually undesired, so the proper way is

derived.prototype = Object.create(baseObj);
derived.prototype.constructor = derived;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Directory doesn't exist, but it does, but it doesn't

分類Dev

Create new object if object doesn't exist in pickle file and load it if it does (best way with multiple objects?)

分類Dev

How to insert the new object to the map only if the object doesn't exist in the map using rust?

分類Dev

How to create a new file via touch if it is in a directory which doesn't exist?

分類Dev

three doesn't exist in Angular

分類Dev

How can I remove displays that don't actually exist?

分類Dev

MongoDB - Projecting a field that doesn't always exist

分類Dev

Append to or create StringSet if it doesn't exist

分類Dev

Deleting a subfield of a field that doesn't exist

分類Dev

Graphql Field doesn't exist on type

分類Dev

Backbone router function goto doesn't exist

分類Dev

Get node value if attribute doesn't exist

分類Dev

Contact_level__c doesn't exist

分類Dev

Adding listener to a value which doesn't exist

分類Dev

Check if session doesn't exist php

分類Dev

Check if session doesn't exist php

分類Dev

Check if string doesn't exist in List

分類Dev

function jumpTo() in owlCarousel 2.0 doesn't exist?

分類Dev

Why doesn't my subclass' variable exist?

分類Dev

Name doesn't exist in current context error

分類Dev

background-size: cover; doesn't actually stretch the image?

分類Dev

VBA produced HYPERLINK formula doesn't actually create workable hyperlink

分類Dev

Why doesn't `std::stringstream::stringstream(std::string&&)` exist?

分類Dev

Check if table exists and if it doesn't exist, create it in SQL Server 2008

分類Dev

Query Postgres JSONB where key doesn't exist

分類Dev

PostgreSQL + IntelliJ : FATAL : role xxx doesn't exist

分類Dev

Avoid error with cat command when src file doesn't exist

分類Dev

Return empty string if the capture group doesn't exist?

分類Dev

Recursive adding file to folder and create folder if it doesn't exist

Related 関連記事

  1. 1

    Directory doesn't exist, but it does, but it doesn't

  2. 2

    Create new object if object doesn't exist in pickle file and load it if it does (best way with multiple objects?)

  3. 3

    How to insert the new object to the map only if the object doesn't exist in the map using rust?

  4. 4

    How to create a new file via touch if it is in a directory which doesn't exist?

  5. 5

    three doesn't exist in Angular

  6. 6

    How can I remove displays that don't actually exist?

  7. 7

    MongoDB - Projecting a field that doesn't always exist

  8. 8

    Append to or create StringSet if it doesn't exist

  9. 9

    Deleting a subfield of a field that doesn't exist

  10. 10

    Graphql Field doesn't exist on type

  11. 11

    Backbone router function goto doesn't exist

  12. 12

    Get node value if attribute doesn't exist

  13. 13

    Contact_level__c doesn't exist

  14. 14

    Adding listener to a value which doesn't exist

  15. 15

    Check if session doesn't exist php

  16. 16

    Check if session doesn't exist php

  17. 17

    Check if string doesn't exist in List

  18. 18

    function jumpTo() in owlCarousel 2.0 doesn't exist?

  19. 19

    Why doesn't my subclass' variable exist?

  20. 20

    Name doesn't exist in current context error

  21. 21

    background-size: cover; doesn't actually stretch the image?

  22. 22

    VBA produced HYPERLINK formula doesn't actually create workable hyperlink

  23. 23

    Why doesn't `std::stringstream::stringstream(std::string&&)` exist?

  24. 24

    Check if table exists and if it doesn't exist, create it in SQL Server 2008

  25. 25

    Query Postgres JSONB where key doesn't exist

  26. 26

    PostgreSQL + IntelliJ : FATAL : role xxx doesn't exist

  27. 27

    Avoid error with cat command when src file doesn't exist

  28. 28

    Return empty string if the capture group doesn't exist?

  29. 29

    Recursive adding file to folder and create folder if it doesn't exist

ホットタグ

アーカイブ