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 does JS create objects?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how JS creates objects". Friends who are interested may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how JS creates objects."

There are 4 ways to create objects:

1. Create objects literally

two。 Create an object using new characters

3. Custom constructor

4. Factory mode to create objects

Object means that it has properties and methods.

1. Literally create objects var person1= {name:' Conan', age:12, sex:' male', eat:function () {console.log ("I want to eat")}, say:function () {console.log ("my name is Conan")} console.log ("person.eat output function", person.eat) person.eat () / / directly output that I am hungry and want to eat

We need to pay attention to the difference between person.eat and person.eat ().

Person.eat is the output function

Person.eat () is the calling function

two。 Create the object var person2=new Object () using the new character; person2.name=' Conan 'person2.age='21'person2.sex=' male' person2.eat=function () {console.log ("I'm hungry, want to eat")} person2.say=function () {console.log ("my name is Conan")} console.log ('gender', person2.sex) / / output male

The disadvantages of the above two ways to create objects:

Although both of the above can be used to create objects

But we have no idea what type of object we created.

Of course, we know they're OBject types.

At this point, we need to use custom constructors to create objects.

3. Custom constructor creates the object function Person (name,age,sex,like) {console.log ('undeined',like without passing a value to the like parameter) / / the this here points to the object Person, console.log (' this',this) this.name=name; this.age=age; this.sex=sex. This.say=function () {console.log ("my name", name)} / / this line of code represents creating an object / / simultaneously instantiating an object / / and initializing the properties of the object / / so this line of code is not simple let per1=new Person ('Conan', 1914 'male'); per1.say (); console.log (per1 instanceof Person); / / true

Now that we know that per1 belongs to the Person type, this is the advantage of custom constructors in creating objects, and we know which type of objects it creates.

Ps: constructors are usually capitalized

When we new an object, we do four things:

By creating custom objects, we understand that when we new an object, we do four things.

1. Open up space to store the current object

two。 Set this as the current object

3. Set properties and methods

4. Return the this object

4. Factory mode creation object function createObj (name,age) {let obj=new Object (); obj.name=name; obj.age=age; obj.sayHi=function () {console.log (obj.name)} return obj;} let per=createObj ('Ito', 200) console.log (per.age); / / 200per.sayHi () / / so far, I believe you have a deeper understanding of "how JS creates objects". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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