Method 1:
var object name =new object ();
Object name. property name = value; Add a property to an object
The name of the object. Property name =function () {} to add a method to the
var box=new boject ();
Box.name= "Li";
box.age=18; Numbers without quotes
Box.run=function () {
Box.name+box.age
}
The method used:
对象.属性名
Method 2:
{} Custom objects in curly braces
In curly braces, multiple sets of attributes, which we must use, are separated by commas.
var object name = {
Property Name: Value,//Add property to Object
Property name (function name): function () {}//Add method to Object
}
The method used:
对象.属性名
var box{
Name: "Uer",
Age:12,
Run:function () {}
}
The way to use it is, box.name
Method 3
A custom object in function form (the function is an object in JS)
function Object Name () {
this. property name = value; Adding properties to an object
this. Function name = function () {}//Add method to Object
}
How to use
var 变量名 = new 对象名() // 创建一个对象的实例 变量名.属性名
function box(){
this.name="user";
this.age=30;
}
使用方法
var box2= new box();
How JavaScript creates objects