JavaScript Object-oriented
An object is a reference type, and the value of an object is an instance of a reference type. A reference type is a data structure in JavaScript that organizes data and functionality together. It is often called a class, but there is no class concept in JavaScript. Although JavaScript is an object-oriented language, it does not have the basic structure of classes and interfaces supported by traditional object-oriented languages.
object creation and common operations
var New Object (); // use the new operator to create an object user.name = ' mouse '; // Adding an attribute to an object user.age = 22
- Object literal (JSON mode)
var user = { name:' Zhang San ', age :+, address:' Chengdu ' };
- Simple Way (Traditional assignment method)
var user == ' John Doe '; // adding properties to an object user.age =' Chengdu, Sichuan ';
- The invocation of the property
There are two ways to call object properties:
Alert (User.Name + "" +user.age); // back to ' programming prodigal Sichuan Chengdu '
Another way:
Alert (user[' name '] + "" +user[' age '); // back to ' programming prodigal Sichuan Chengdu '
- Add Method
varuser ={name:' Programming Prodigal ',//adding properties to an objectage:22, Address: ' Chengdu, Sichuan ', Showinfo:function(){ //Add a methodAlert This. name+ "" + This. age+ "" + This. Address); }, Showhello:showhello//add a method outside an object to an object}; functionShowhello () {alert ("Hello!"); } user.showinfo ();//Call Method User.showhello ();
JavaScript Object-oriented (i)