(String instanceof Object) returns true, but I cannot find Object.prototype in the String.prototype chain

My JavaScript says that the operator instanceofsearches if the prototype property is present in the constructor or not.

String instanceof Object; // true
String.prototype                     === Object.prototype; // false
String.prototype.prototype           === Object.prototype; // false
String.prototype.prototype.prototype === Object.prototype; // Throws into "Cannot read property 'prototype' of undefined"

String.prototype.prototype; // undefined

It just means that Object.prototype is not in the String.prototype chain. What then confused me, and why String instanceof Objectreturns true?

+4
source share
2 answers

Your JavaScript does not lie to you: D. It is true that the operator instanceofchecks to see if the constructor prototype property exists in the object prototype chain or not.

, JavaScript. .

String instanceof Object;//true

String , . :

String.__proto__ === Object.prototype;//

String.__proto__.__proto__ === Object.prototype;//

'true' :) , Object.prototype String.

: " proto "

The use of __proto__ is controversial, and has been discouraged by many. It was never originally included in the EcmaScript language spec, but modern browsers decided to implement it anyway. Today, the __proto__ property has been standardized in the ECMAScript 6 language specification and will be supported into the future. Still, mutating the [[Prototype]] of an object is a slow operation that should be avoided if performance is a concern.

, proto, :

Object.getPrototypeOf(String.prototype) === Object.prototype;

, . JavaScript.

+4

, String instanceof Object, String - , - JavaScript.

prototype , . , , new . ( , .) String Function.prototype ( String - ), ES5 getPrototypeOf:

Object.getPrototypeOf(String) === Function.prototype // true

ES6 __proto__ ( ), , ):

String.__proto__ === Function.prototype // true

, , String - ; Object? , :

String.__proto__.__proto__ === Object.prototype // true
Object.getPrototypeOf(Object.getPrototypeOf(String)) === Object.prototype // true

, .prototype.prototype .__proto__.__proto__:

snippet.log(String.__proto__ === Function.prototype);         // true
snippet.log(String.__proto__.__proto__ === Object.prototype); // true
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.imtqy.com/simple-snippets-console/snippet.js"></script>

:

// NOTE! Snippet requires a modern browser that has the ES6 __proto__
// property! Current Chrome, Firefox, and IE all do.

function GrandParent() {
}

function Parent() {
  GrandParent.call(this);
}
Parent.prototype = Object.create(GrandParent.prototype);
Parent.prototype.constructor = Parent;

function Child() {
  Parent.call(this);
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

var c = new Child();
snippet.log(c.__proto__ === Child.prototype);                           // true
snippet.log(c.__proto__.__proto__ === Parent.prototype);                // true
snippet.log(c.__proto__.__proto__.__proto__ === GrandParent.prototype); // true
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.imtqy.com/simple-snippets-console/snippet.js"></script>
+3

Source: https://habr.com/ru/post/1583959/


All Articles