[ Web Proxy ]
URL:
Viewing: https://learn.javascript.ru/function-prototype [Back]  [Original]

F.prototype
:
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
24 2024 .

F.prototype

, - new F().

F.prototype , new [[Prototype]] .

:

JavaScript . .

, , . "prototype" -, . .

, F.prototype "prototype" F. , F .

:

let animal = {
  eats: true
};

function Rabbit(name) {
  this.name = name;
}

Rabbit.prototype = animal;

let rabbit = new Rabbit("White Rabbit"); //  rabbit.__proto__ == animal

alert( rabbit.eats ); // true

Rabbit.prototype = animal : new Rabbit() animal [[Prototype]].

:

[]

: "prototype" , "F", [[Prototype]] , rabbit animal.

F.prototype new F

F.prototype new F [[Prototype]] .

F.prototype (F.prototype = < >), , new F, [[Prototype]] , .

F.prototype , constructor

( ) "prototype".

"prototype" constructor, -.

:

function Rabbit() {}

/*   
Rabbit.prototype = { constructor: Rabbit };
*/
[]

:

function Rabbit() {}
//  :
// Rabbit.prototype = { constructor: Rabbit }

alert( Rabbit.prototype.constructor == Rabbit ); // true

, , constructor [[Prototype]]:

function Rabbit() {}
//  :
// Rabbit.prototype = { constructor: Rabbit }

let rabbit = new Rabbit(); //   {constructor: Rabbit}

alert(rabbit.constructor == Rabbit); // true (   )
[]

constructor .

:

function Rabbit(name) {
  this.name = name;
  alert(name);
}

let rabbit = new Rabbit("White Rabbit");

let rabbit2 = new rabbit.constructor("Black Rabbit");

, , , (, ), .

, , "constructor" ,

JavaScript "constructor".

, "prototype" , .

, , "constructor" .

:

function Rabbit() {}
Rabbit.prototype = {
  jumps: true
};

let rabbit = new Rabbit();
alert(rabbit.constructor === Rabbit); // false

, "constructor", // , :

function Rabbit() {}

//   Rabbit.prototype ,
//     
Rabbit.prototype.jumps = true
//    ,        Rabbit.prototype.constructor

constructor:

Rabbit.prototype = {
  jumps: true,
  constructor: Rabbit
};

//   constructor  ,     

[[Prototype]] , -. , .

. :

  • F.prototype ( [[Prototype]]) [[Prototype]] new F().
  • F.prototype , null. .
  • "prototype" , -, new.

prototype - :

let user = {
  name: "John",
  prototype: "Bla-bla" //    -  
};

F.prototype = { constructor: F }, "constructor".

: 5

new Rabbit, .

:

function Rabbit() {}
Rabbit.prototype = {
  eats: true
};

let rabbit = new Rabbit();

alert( rabbit.eats ); // true
  1. ( ). alert ?

    function Rabbit() {}
    Rabbit.prototype = {
      eats: true
    };
    
    let rabbit = new Rabbit();
    
    Rabbit.prototype = {};
    
    alert( rabbit.eats ); // ?
  2. ( )?

    function Rabbit() {}
    Rabbit.prototype = {
      eats: true
    };
    
    let rabbit = new Rabbit();
    
    Rabbit.prototype.eats = false;
    
    alert( rabbit.eats ); // ?
  3. ( )?

    function Rabbit() {}
    Rabbit.prototype = {
      eats: true
    };
    
    let rabbit = new Rabbit();
    
    delete rabbit.eats;
    
    alert( rabbit.eats ); // ?
  4. , , :

    function Rabbit() {}
    Rabbit.prototype = {
      eats: true
    };
    
    let rabbit = new Rabbit();
    
    delete Rabbit.prototype.eats;
    
    alert( rabbit.eats ); // ?

:

  1. true.

    Rabbit.prototype [[Prototype]] , .

  2. false.

    . Rabbit.prototype, , Rabbit.prototype, [[Prototype]] rabbit.

    , , .

  3. true.

    delete , . delete rabbit.eats eats rabbit, . , .

  4. undefined.

    eats , .

: 5

, obj, - , .

?

let obj2 = new obj.constructor();

- obj, . -, .

, , "constructor" .

, "prototype", , , , :

function User(name) {
  this.name = name;
}

let user = new User('John');
let user2 = new user.constructor('Pete');

alert( user2.name ); // Pete (!)

, User.prototype.constructor == User.

- User.prototype "constructor", User, .

:

function User(name) {
  this.name = name;
}
User.prototype = {}; // (*)

let user = new User('John');
let user2 = new user.constructor('Pete');

alert( user2.name ); // undefined

user2.name undefined?

, new user.constructor('Pete'):

  1. constructor user. .
  2. . user User.prototype, .
  3. , User.prototype {}, Object.prototype.
  4. , Object.prototype Object.prototype.constructor == Object. , constructor - .

let user2 = new Object('Pete').

, , . new User, new Object. .

( , , new Object(...) . , new Object , , new Object ).


Web Proxy Viewer  |  New URL  |  Original Page