Get the App
SLTechnology News&Howtos  ›  Development  › 

How does JS create objects

Shulou Source: shulou.com Published: 2022-06-01 19:04:59 10月04日 Update

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!

Tags: Functions objects methods identical properties code advantages prototypes disadvantages dependencies content parameters patterns changes learning utility deeper interests variables characters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info NVidia Huawei Apple