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

Can JavaScript create objects?

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

Share

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

Most people do not understand the knowledge points of this article "can JavaScript create objects?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "JavaScript can create objects" article.

JavaScript can create objects. Creation method: 1, create an object directly with Object, syntax "new Object ()"; 2, create object with new keyword, syntax "new object name ()"; 3, create object with JSON, syntax "object= {attribute name: attribute value,...}".

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

Can't JavaScript create an object?

A JavaScript object is a variable that can hold many different values. It acts as a container for a set of related values. For example, users of a website and bills in a bank account can all be JavaScript objects.

In JavaScript, an object contains two values: a property and a method.

When you create a JavaScript object, you need to define its name, properties, and methods.

The method to create a JavaScript object:

1. Use Object to create objects directly

Var myObj=new Object (); / / create an empty object myObj.name= "wangshihcheng" using the Object class; myObj.age=20; myObj.infor=function () {[xss_clean] ("name:" + this.name); / / this. Point to the problem [xss_clean] ("age:" + this.age);} myObj.infor (); / / call the method in the created object

2. Use the new keyword to call the constructor to create an object

The code is as follows:

Var obj = new Object (); obj.name = "Kitty"; / / add attributes obj.age = 21 for the object; obj.showName = function () {/ / add method console.log (this.name) for the object;}; obj.showAge = function () {console.log (this.age);}; obj.showName (); obj.showAge ()

This method generates an object through the new keyword, and then constructs an object by adding properties and methods based on the fact that JavaScript is a dynamic language. Where this represents the object that calls the method.

The problem with this approach is that if we need to create objects multiple times, we need to repeat the code many times, which is not conducive to code reuse.

3. Use prototype pattern to create objects

In JavaScript, each function has a prototype property, which is a pointer to an object called

A prototype object that contains properties and methods that can be shared by all instance objects of a particular type

In addition, this object has a built-in property constructor that points to the constructor that creates the object

When we use the prototype pattern, we can make all instances share the properties and methods of the prototype object.

Therefore, it is not necessary for us to define the information of the instance of the object in the early constructor.

Function Student () {} Student.prototype.name= "wang"; Student.prototype.sex= "man"; Student.prototype.class= "5"; Student.prototype.sayName=function () {console.log (this.name);} var s1=new Student (); s1.sayName (); / / wang var s2=new Student (); s2.sayName (); / / wang s2.name = "shicheng"; s2.sayName (); / / shicheng

When we read the properties of an object, we perform a search, starting with the object instance itself

If this property is found in the instance, the search ends and the value of the property is returned

If it is not found on the instance, continue to extend to the prototype object of the object, and search for the prototype object of the object, if on the prototype

If this property is found, the value corresponding to the property above the prototype is returned. If not, undefine is returned.

Therefore, you can see that the instance object properties override the properties above the prototype object

4. Use JSON to create objects

/ / object= {attribute name 1: attribute value 1, attribute name 2: attribute value 2pm.}

/ / Note that attribute names should be enclosed in double quotation marks in JOSN format

Var p = {"name": "wangsch", "gender": "man", "age": 40, "son": [{"name": "son1", "age": 2}, {"name": "son2", "age": 5}], "infor": function () {[xss_clean] ("Father's name:" + this.name+ ") Father's age: "+ this.age+"

For (var child in this.son) {[xss_clean] ("son's name:" + this.son [child] .name + ", son's age:" + this.son [child] .age + "

");} p.infor (); / / call the infor method in object p

5. Combine constructors and prototype patterns to create objects

The constructor is used to define the properties of the instance, and the prototype pattern is used to define methods and shared properties.

Function Student (name,sex,grade) {this.name=name; this.sex=sex; this.grade=grade;} Student.prototype.sayName=function () {console.log (this.name);} Student.prototype.school= "nongda"

This mixed mode supports passing parameters to the constructor and saves a lot of memory.

6. Use dynamic prototyping method to create objects.

The code is as follows:

Function Person (name,age) {this.name = name; this.age = age; this.array = new Array ("Kitty", "luo"); / / if _ initialized in the Person object is undefined, the method if (typeof Person._initialized = = "undefined") {Person.prototype.showName = function () {console.log (this.name) has not been added to the prototype of Person }; Person.prototype.showArray = function () {console.log (this.array);}; Person._initialized = true;}} var obj1 = new Person ("Kitty", 21); var obj2 = new Person ("luo", 22); obj1.array.push ("Wendy") / / add an element obj1.showArray () to the array attribute of obj1; / / Kitty,luo,Wendy obj1.showName (); / / Kitty obj2.showArray (); / / Kitty,luo obj2.showName (); / / luo

This approach is more or less the same as the constructor / prototype approach. Only the method is added to the constructor, and an attribute is added to the constructor Person to ensure that the if statement can only be executed successfully once. In practical applications, the most extensive constructor / prototype method is used. The dynamic prototyping method is also popular and is functionally equivalent to the constructor / prototype method. Do not use constructors and prototype methods alone.

The above is about the content of this article on "can JavaScript create objects?", I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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