JavaScript prototype confusion

1.

I read Douglas Crockford's book, Good Details. The book is methodadded toFunction.prototype

a.

Function.prototype.method = function() {}

Function.prototype.method === Function.method; // true

b.

function MyClass() {}

MyClass.prototype.method = function() {}

MyClass.prototype.method == MyClass.method // false

infact MyClass.method- undefined.

Why is this true in step a and false in step b?

2.

please explain the following:

Object instanceof Function    // true
Function instanceof Object    // true
Object instanceof Object      // true
Function instanceof Function  // true

This is very confusing. explain in simple words.

+4
source share
2 answers

1a.

01 Function.prototype.method = function foo() {}
02 Function.prototype.method === Function.method; // true

This is adding a property to the object that the property of the prototypeembedded object points to Function.

Function - - "". Function - , - . Function , prototype. -.

1 method Function.prototype, - foo ( ).

2 Function.prototype.method ( 1) , method, - Function. JavaScript , .

, 2 Function.prototype.method , method Function.prototype.

2 Function.method Function; , , Function.__proto__ ( ). __proto__ JavaScript prototype , . , ( !), , Function " " ( Function , userland), , prototype __proto__ - - .

, method __proto__, , Function.prototype, method. :

02 Function.prototype.method === Function.method; // true

1b.

01 function MyClass() {}    
02 MyClass.prototype.method = function foo() {} // Named for clarity.
03 MyClass.prototype.method == MyClass.method // false 
04 // infact MyClass.method is undefined.

, , Function , 1a.

( 1 ), MyClass prototype , .

2 method foo.

3 foo method MyClass. , method MyClass . MyClass, , __proto__, method ( , ). , 1a ( JavaScript), __proto__ prototype , . MyClass Function, MyClass.__proto__ Function.prototype ( , MyClass.prototype). . method, undefined.

:

MyClass.prototype.method == MyClass.method // false

2.

Object instanceof Function    // true

- -- . , .


Function instanceof Object    // true

instanceof - , true, RHS LHS. :

Function.__proto__ -> Function.prototype
Function.prototype -> function f() {} // An ordinary function, named for clarity
f.__proto__ -> {} // An ordinary object. Let call it 'o'.

, , , Object f.__proto__ ( f). :

Function instanceof Object    // true

Object instanceof Object      // true

, Object.prototype Object.

Object.__proto__ -> Function.prototype 
Function.prototype.__proto__ -> {} // This is 'o' from above.

So Function.prototype.__proto__ === Object.prototype, :

Object instanceof Object      // true

Function instanceof Function  // true

, 1a.

+1

1.

a) , , , , .

b) MyClass , MyClass . MyClass MyClass .

2.

. , . Object, , Object - .

, Function Object. . , . .

, Object Number, , Number Object, Function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

, , (, URIError).

  • Boolean
  • Math
  • RegExp
  • JSON
+4

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


All Articles