Location>code7788 >text

Handwritten js new, what really happens in the process of new

Popularity:117 ℃/2024-10-28 17:35:17

Chestnut Bibimbap

  • blogosphere
  • fig. beginning
  • new essays
  • liaison
  • subscribe to
  • managerial

Handwritten js new, what really happens in the process of new

In JavaScript, the application of the new keyword can be said to be more than usual, the most basic new Array (), new Set (), and then is new a constructor created by themselves, that is, to create an example of the constructor. Such as: var person1 = new Person ("an apple", 18); but do you really understand new and its underlying principles, this article will give a few examples and handwritten a new to take you in-depth understanding.

new

Usage of new

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

const car1 = new Car('Eagle', 'Talon TSi', 1993);

();   //Output: "Eagle"

So what happened to the new process?

  • Create an empty object: Creates an empty simple JavaScript object. For convenience, we'll call thisnewInstance

  • Specify the prototype chain: WillnewInstance The [[Prototype]] of the [[Prototype]] points to the constructor'sprototype attribute, otherwise thenewInstance will remain a normal object with a [[Prototype]] of

  • Change this to point to: Execute the constructor with the given parameters andnewInstance The context to bind to this.

  • return value: ReturnnewInstance。

 

Handwrite a new

function myNew(Fun, ...args) {
    let obj = {}
    obj.__proto__ =
    (obj, args)
    return obj
}
function Person(name,age) {
     = name
     = age
}
let apple = myNew(Person, 'An apple.','18')
(); //exports:An apple.
(); //exports:18
  • ...args: Put all the remaining elements into theargscenter

  • obj. __ proto __ = : Willobj The prototype chain points to the constructor'sprototype causality

  • (obj, args): binds this inside the constructor to theobjon it and executes the constructor

With this sequence of operations, we can get the constructor'snamerespond in singingageProperties.

It looks like it's done, but it's actually missing a very important point.

Recall that when we create an object through the new function, what else do we do besides referencing its properties?
let arr = new Array()
(4)
(3)
(arr);

Of course it is to use its own methods, but the above does not give a reference on the method. This will have to introduce the knowledge of the prototype chain, we just need to add to the prototype chain of Person's methods we want to be able to realize the code is as follows:

function myNew(Fun, . .args) {
    let obj = {}
    obj.__proto__ =
    (obj, args)
    return obj
}
function Person(name,age) {
     = name
     = age
}
 = function () {
    ('I am an instance object of Person'); }
}
 = function () {
    ()
}
let p = myNew(Person, 'an apple')
(); // Output: I am an instance object of Person
(); // Output: an apple
(); // Output: an apple
That's our final answer, give it to the interviewer, PERFECT!
posted @ 2024-10-28 17:17  Chestnut Bibimbap Read (0) Comments (0compiler  favorite  report (malefactors to the police)
refresh pageBack to top

bulletin

Copyright © 2024 Chestnut Bibimbap
Powered by .NET 8.0 on Kubernetes
'); // $(".calendar-img").html(calendarArrimg[calendarIdx].img); // $(".calendar-img").fadeIn(2000) // },2000) // }else{ // setTimeout(function(){ // calendarIdx++; // if(calendarIdx>6) calendarIdx=0; // $(".calendar-img").html(calendarArrimg[calendarIdx].img); // $(".calendar-img").fadeIn(2000) // },2000) // } // } /* Mouse Effects */ var a_idx = 0; $("#mainContent").css("z-index",'0') jQuery(document).ready(function($) { setTimeout(function(){ $.awesCnb(opts)},3000) // setInterval(function(){ // if($(".event-wrapper").find(".calendar-img").length){ // $(".calendar-img").fadeOut(2000) // }else{ // $(".event-img").fadeOut(2000); // } // switchCalendarImg() // },5000) $("body").click(function(e) { var a = new Array("(●'◡'●)ノ♥","❤❤❤","( ゚∀゚) ノ❤","。◕‿◕。 ⓦⓤⓗⓤ~","✿◕‿◕✿ ❤❤","❤❤(≖ ◡ ≖✿)","(。◕ˇ∀ˇ◕)"," (●´ω`●)φ","(ง •̀_•́)ง❤","☺☻☺☻☺☻","ⓗⓐⓟⓟⓨ❤","ヾ(=^▽^=)ノ❤❤"); var $i = $("").text(a[a_idx]); a_idx = (a_idx + 1) % ; var x = , y = ; $({ "z-index": 9999999999999999999999999999999999999999999999999999999999999999999999999, "top": y - 20, "left": x, "position": "absolute", "font-weight": "bold", "white-space": "nowrap", "color": "rgb("+~~(255*())+","+~~(255*())+","+~~(255*())+")" }); $("body").append($i); $({ "top": y - 180, "opacity": 20 }, 1500, function() { $(); }); }); });