Defining objects: The combination of properties and methods (combinations of variables and functions)
1. (* * *) var obj = {}
2.var obj = new Object ();
3. Defining objects with function
Specific examples are:
1 //1. Defining an Obj Object2 //obj: Object name3 var obj = {4 //Height,weight,sex properties called Objects5 height:170,//use, separating properties and methods6 weight:90,7 sex: ' BBW ',8 9 //Say (): Method called ObjectTen say:function () { One alert (' 付帅帅, you have a long handsome '); A }, - eat:function () { - alert (' eat '); the } - } - - //Gets the object's properties (object name. Property name) + Console.log (obj.weight); - //method of the Calling object (object name. Method Name ()) +Obj.say ();
1 //2. Defines an empty object2 var O1 = new Object ();3 //Assigning properties to an object4 o1.height = ' 150cm ';5 o1.color = ' Silver ';6 O1.fire = function () {7 alert (' Chop it, when firewood Burns ');8 }9 Ten //Call One Console.log (o1.height); AO1.fire ();
3. Use the function to define the Object function demo () { //This represents the current object this.memory = ' 16G '; This.hard = ' 1T '; This.ys = function () { alert (' The computer is doing a high-load operation at this moment ');} } Call (Instantiate demo () function) var d = new demo (); Console.log (d.memory); D.ys ();
The three most common methods of JS object definition