There is a concept of class in an object-oriented language (such as Java), and through classes, you can create as many objects with the same properties and methods. However, JavaScript does not have the concept of a class, so its objects are also different from those in a class-based language.
To understand object orientation, you must first understand objects and prototypes in Javascript. In this article, we mainly learn about objects in Javascript.
About the object
An object is actually a reference type. The value of the object is an instance of the reference type. Reference types in JavaScript are data structures that organize data and functions together. It is also often called a class, but there is no concept of a class in JavaScript. Although JavaScript is an object-oriented language, it does not have the basic structures such as classes and interfaces supported by traditional object-oriented languages.
Creation of two objects and common operations
1. Use the new operator
<script>var user = new Object(); //Create an object using the new operator user.name = 'Recited on the Triangle Lake'; //Add attributes to the object user.age = 22;user.address = 'Hubei Wuhan';alert(user.name + " " +user.age); //Return 'Recited on the Triangle Lake' Hubei Wuhan'</script>
Note: In the above method, the new keyword can be omitted, that is, var user = new Object(); is equivalent to var user = Object();
2.JSON method creation
For some knowledge about JSON, you can search on Baidu.
Simple JSON object: {name:'Remembering on the Triangle Lake',age:22,address:'Hubei Wuhan'}
//Create/*Simple JSON object using JSON method: {name:'Read on the Triangle Lake', age:22, address:'Hubei Wuhan'}*/var user = {name:'Read on the Triangle Lake', age:22, address:'Hubei Wuhan' };alert(user.name + " " +user.age);//Return on the Triangle Lake', Hubei Wuhan'3. Traditional assignment method
//Traditional assignment var user = {};user.name = 'Remember on the Triangle Lake'; //Add attributes to the object user.age = 22;user.address = 'Hubei Wuhan';alert(user.name + " " +user.age);//Remember 'Remember on the Triangle Lake' Hubei Wuhan'4. Calling of properties
There are two ways to call object properties:
Take the example above for example. We have used one of the methods above, that is, the '.' operator, and the calling method is as follows:
alert(user.name + " " +user.age);//Return 'Remembering in the shores of Triangle Lake, Hubei Wuhan'
Another way:
alert(user['name'] + " " +user['age']);//Return 'Remembering in the shore of Triangle Lake, Hubei Wuhan
5. Add methods to the object
Adding methods to objects and adding a property to variables is actually similar. The specific code is as follows:
var user = {name:'Read on the Triangle Lake', //Add attribute age:22,address:'Hubei Wuhan', showInfo:function(){//Add a method alert(this.name+" "+this.age+" "+this.address);//Return 'Read on the Triangle Lake', 22 Hubei Wuhan' }, showHello:showHello//Add the external method of the object to the object};function showHello(){alert("Hello!"); }user.showInfo();//Calling the method user.showHello();6. Delete the object's properties
Use the delete operator to delete the attributes of the object. Format: delete object name. Attribute name
var user = {name:'Read on the Triangle Lake', //Add attribute age:22, address:'Hubei Wuhan'};alert(user.name);//Return 'Read on the Triangle Lake'delete user.name;//Delete user's name attribute alert(user.name);//Return 'undefined'Three summary
Here we briefly describe the creation of objects, the addition of object methods and the deletion of object properties in Javascript. Objects are a relatively important and basic part in JavaScript object-oriented. Only by clarifying some common operations of objects can object-oriented development be carried out.
The above is the objects and prototypes in JavaScript introduced to you by the editor (I). I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!