In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to create objects with JavaScript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to create objects with JavaScript".
Object: specifically refers to something in the natural world, with some characteristics (attributes) and behavior (methods), such as Yao Ming, the object has the characteristics of name, gender, height, ball number, team, etc., and can dribble, shoot, run, eat, etc., so how do we use js to create objects?
The first is to create instance objects literally.
Var YaoMing = {name: "YaoMing", age:36, gender: "male", eat:function () {console.log ('stinky tofu');}, read: () {console.log ('madman on the left and genius on the right');}}
The second kind: call the system constructor to create the object
Var per2=new Object (); per2.name= "Orojimaru"; per2.age=30; per2.sex= "male"; per2.eat=function () {console.log ("eating durian");}; per2.play=function () {console.log ("this little snake is really funny");}
At this point, check whether per2 is an instance of Object and return true.
The third: custom constructor to create objects
Function Person (name, age, gender) {this.name = name; this.age = age; this.gender = gender; this.play = function () {console.log ("playing games every day");};} var per = new Person ("maiden", 18, "female") Console.log (per instanceof Person); / / returns true
Now think about the third custom constructor to create an object. What is the internal implementation process?
Take the example above, creating an object var per = new Person ("Makita", 18, "female"); instantiating an object while also initializing the property age,name,gender assignment. The internal process is as follows:
* 1. Open up spatial storage objects
* 2. Set this as the current object
* 3. Set the values of properties and methods
* 4. Return the this object
Fourth: factory mode to create objects
/ / Factory mode creation object function createObject (name,age) {var obj=new Object (); obj.name=name; obj.age=age; obj.sayHi=function () {console.log (Hello);}; return obj;} / / Custom constructor creation object function Person (name,age) {this.name=name; this.age=age This.sayHi=function () {console.log ("Hello");};}
Factory mode creation object: var per1=createObject (Xiaoming, 20)
Custom constructor to create object: var per2=new Person (Xiao Hong, 20)
By comparing the above two ways of creating objects, we can draw the following conclusions:
Factory mode to create objects
1. Function name is lowercase, 2. The new keyword is used inside the function, 3. And there is a return value, the object after 4.new is the current object, 5. You can create an object by calling the function directly.
Custom constructor to create object
1. The initials of the function are capitalized, 2. The new keyword is not used inside the function, 3. There is no return value, 4.this represents the current object, 5. Create objects by means of new.
Thank you for reading, the above is the content of "how to create objects with JavaScript". After the study of this article, I believe you have a deeper understanding of how to create objects with JavaScript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.