The code is very simple, no more nonsense.
//The first way to define
var person=new Object(); //An object is created.
person.name="tom"; //Use the person object to call the name attribute, its value is tom
alert(person.name); //Display name attribute value
person.say=function(){ //Adds a say function to the person object.
alert("person say");
};
person.say();
//The second definition method
var person={
name:"tom",
say:function(){
alert("hello person");
}
}; //An object is created.
//alert(person.name);
//person.say();
person.age=10;
alert(person.age);
//Defining classes in js uses functions.
var Person = function(name){ //We are defining a class. It is equivalent to having a constructor with parameters.
this.name =name; //Class attributes
this.say = function(){ //Class method.
alert("say good");
}
}
var p = new Person("fox"); //Define an object p
of the Person class
alert(p.name); //Call the name attribute