In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to create objects in JS". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how JS creates objects".
1. New Object ()
Var x = "age" var obj=new Object (); obj.name= "wang"; obj.x=20; / /. String obj [x] = 25; / / [variable] console.log (obj); / / {name: "wang", x: 20, age: 25}
2. Literal quantity
Var a = "hobby" var obj= {"name": "wang", "age": "18"}; obj.sex= "male"; obj [a] = "singing"; obj.say=function () {} console.log (obj) / / {name: "wang", age: "18", sex: "male", hobby: "singing", say: singing} var obj2= {"aa bb": "hellow", ".x": "world"}; console.log (obj2 ["aa bb"]); / / hellow console.log (obj2 [".x"]); / / world
III. Factory model
/ / 1. Create the function function a (name, age) {var obj = {"name": "wang", "age": 19, "say": function () {}} return obj} / / 2. Call var obj1 = a ("wang", 20); console.log (obj1); / / {name: "wang", age: 19, say: reply} console.log (obj1 instanceof Object); / / true / / advantages: return new objects, do not affect each other / / disadvantages: code repetition (same method). / / No affiliation
Fourth, the constructor
/ / fourth, constructor; / / advantages: subordinate / / disadvantages: code duplication (the same method); / / 1. Create the function / / 2. Pass in the parameter function A (name,age) {/ / 3.this. Attribute name = value this.name=name; this.age=age; this.say=function () {}} / / call: var obj=new constructor (parameter) var obj=new A ("wang", 19); console.log (obj); console.log (obj instanceof Object); / / true console.log (obj instanceof A); / / true
5. Prototype model
/ / prototype advantages: common / same properties, methods do not repeat have dependency / / disadvantages: the properties on the prototype can not be changed alone function Fn () {} Fn.prototype.name= "king"; Fn.prototype.age=19; var obj=new Fn (); console.log (obj) / * modify obj.__proto__.name obj.__proto__.name change obj2.__proto__.name also change obj and obj1 share _ _ proto__ object common / same properties, The method is put on the constructor .prototype to implement that the code does not repeat * / obj.__proto__.name= "Li" console.log (obj.__proto__.name) Var obj2=new Fn (); console.log (obj2); console.log (obj.__proto__.name); now that you have a better understanding of "how JS creates objects", you might as well do it! 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.