Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the new method in javascript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use the new method in javascript". In the daily operation, I believe that many people have doubts about how to use the new method in javascript. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the new method in javascript". Next, please follow the editor to study!

In JavaScript, the new operator is used to create an instance of a custom object, or an instance of a constructor built-in object; the instance created by new can access properties in the constructor Person.

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

What is the use of new in javascript

The purpose of new

The new:new operator is used to create an instance of a custom object, or an instance of a constructor built-in object. What do you mean? it's a bit of a mouthful. Let's take a look at the chestnut first.

What happened when new F ()

First edition

Here are the chestnuts:

Function Person (name, age) {this.name = name this.age = age console.log (this) / / Person {name: "xuedinge", age: "20"} Person.prototype.have = "cat" const person = new Person ("xuedinge", "20") console.log (person.name) / / xuedingeconsole.log (person.have) / / catconsole.log (person) / / Person {name: "xuedinge", age: "20"}

From this chestnut, we can see that new has the following capabilities:

1. The instance created by new can access the properties in the constructor Person.

2. The instance created by new can access the properties on the constructor prototype.

3. New can bind this in the constructor to the newly created object

Let's first implement these three capabilities of new:

Function fakeNew (Fn) {/ / create an empty object let obj = new Object () / / point the prototype pointer of the new object to the prototype of the constructor obj.__proto__ = Fn.prototype / / handle the remaining parameters except Fn Fn.apply (obj, [] .slice.call (arguments, 1)) return obj}

Let's see the effect.

Function Person (name, age) {this.name = name this.age = age console.log (this) / / Person {name: "xuedinge" Age: "20"} Person.prototype.have = "cat" function fakeNew (Fn) {/ / create an empty object let obj = new Object () / / point the prototype pointer of the new object to the prototype of the constructor obj.__proto__ = Fn.prototype / / handle the remaining parameters except Fn Fn.apply (obj, [] .slice.call (arguments, 1)) return obj} const newPerson = fakeNew (Person Xuedinge, "20") console.log (newPerson.name) / / xuedingeconsole.log (newPerson.have) / / 20console.log (newPerson) / / Person {name: "xuedinge", age: "20"}

It looks more similar to new's ability. But what does it look like when there is a return value in the constructor? look at another one:

/ / when the returned value is an object: function Person (name, age) {this.name = name this.age = age console.log (this) / / Person {name: "xuedinge", age: "20"} return {car: "leikesasi"} Person.prototype.have = "cat" const person = new Person ("xuedinge") "20") console.log (person.name) / / undefinedconsole.log (person.have) / / undefinedconsole.log (person.car) / / leikesasiconsole.log (person) / / {car: "leikesasi"}

You can see that when the constructor returns an object, the instance object created by new is the result returned by the constructor. When the return value is a normal object, take a look at the following:

/ / when the returned value is normal: unction Person (name, age) {this.name = name this.age = age console.log (this) / / Person {name: "xuedinge", age: "20"} return "leikesasi"} Person.prototype.have = "cat" const person = new Person ("xuedinge", "20") console.log (person.name) / / xuedingeconsole.log (person.have) / / catconsole.log (person) / / Person {name: "xuedinge" Age: "20"}

When the return value is a normal object, the result is the same as when there is no return value.

At this point, the study on "how to use the new method in javascript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report