Method one for declaring an object (directly assigned by a constant value)
varXiaoming = {//declares an object (declared directly by a constant xiaoming in the form of a {} assignment)Name: ' Xiaoming ',//a property property of an object is separated from the property by a comma. AGE:40,//an attribute of the objectSex: ' Male ',//an attribute of the objectJobfunction(){//One method of the objectAlert (' Xiaoming is good at acting '); return' Object method return value '; }} alert (Xiaoming.name); //xiaoming.job ();//method of invoking the Xiaoming object result: Xiao Ming is very good at acting.Alert (Xiaoming.job ());//Object method Return value Result: Object method return value //called if the method property of the object is appended ()
Method two for declaring an object (via new object () declaration)
varXiaoming =NewObject ();//another way to declare an objectXiaoming.name = ' Xiao Ming ';//this method uses the. Property = property value. To represent the properties of an objectXiaoming.age = 40;//declaring a property of a Xiaoming objectXiaoming.sex = ' Male ';//declaring a property of a Xiaoming objectXiaoming.job =function(){//one way to declare a Xiaoming objectAlert (' Xiaoming is good at acting '); } alert (xiaoming.name);//Call Xiaoming A property of this object. Result: Xiao MingXiaoming.job ()//the method that calls xiaoming this object results: Xiao Ming is very good at acting.
Method three for declaring an object (with the object () declaration omitting new)
varXiaoming = Object ();//the third way to declare an object xiaoming. Xiaoming.name = ' Xiao Ming ';//declaring a property of a Xiaoming objectXiaoming.age = 40;//declaring a property of a Xiaoming objectXiaoming.sex = ' Male ';//declaring a property of a Xiaoming objectXiaoming.job =function(){//a method (function) for declaring a Xiaoming objectAlert (' Xiaoming is good at acting '); } alert (Xiaoming.age); //40 calling a property of the Xiaoming object results:Xiaoming.job ();//Call Xiaoming a method (function) of this object result: Xiao Ming is very good at acting.
//Supplement the. property is exactly equivalent to [' property ']
var xiaoming = Object (); xiaoming[' name '] = ' Xiao Ming '; xiaoming[//xiaoming["age", like the Xiaoming.age effect, is a property of an object, but it's just two ways to do it. xiaoming[' sex ' = ' male ' xiaoming[function() { alert (' xiaoming is very good at acting ') ); } Alert (xiaoming[' age '); // + // Xiaoming is a good actor .
Summary: Objects (both to store data and to store functions)
JS Object-oriented