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

prototype
:
Light themeDark theme
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
2022 1 22

prototype

new F() .

' . new [[Prototype]] .

:

.

. "prototype" . .

(F) F.prototype "prototype" F . F.prototype "prototype" ' .

:

let animal = {
  eats: true
};

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

Rabbit.prototype = animal;

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

alert( rabbit.eats ); // true

Rabbit.prototype = animal "new Rabbit [[Prototype]] animal ." .

.

[]

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

F.prototype new F .

F.prototype new F . new F [[Prototype]] .

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

prototype constructor

"prototype" .

( ) "prototype" constructor , constructor .

.

function Rabbit() {}

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

.

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

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

new Rabbit constructor , [[Prototype]] .

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

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

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

constructor constructor .

.

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

let rabbit = new Rabbit(" ");

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

( ) .

"constructor"

"constructor" .

"prototype" . "constructor" .

"prototype" . new "constructor" .

:

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

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

constructor "prototype" "prototype" "prototype" , .

function Rabbit() {}

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

"prototype" constructor constructor .

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

//  constructor       constructor     .

[[Prototype]] . .

.

  • (F.prototype) [[Prototype]] . F.prototype new F() [[Prototype]] .
  • F.prototype null . .
  • new .

"prototype" .

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

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

: 5

new Rabbit Rabbit "prototype" .

.

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

let rabbit = new Rabbit();

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

    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. delete ?

    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 rabbit [[Prototype]] .

    .

  3. true

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

  4. undefined

    eats eats .

: 5

, obj . .

?

let obj2 = new obj.constructor();

. .

"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 User constructor .

:

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. new user.constructor('Pete') user constructor .
  2. . user User.prototype, User.prototype .
  3. User.prototype {}, Object.prototype. Object.prototype.constructor == Object Object .

let user2 = new user.constructor('Pete'); let user2 = new Object('Pete') . Object . let user2 = new Object('Pete') let user2 = {} . user2.name undefined .

.

Web Proxy Viewer  |  New URL  |  Original Page