Create a method summary for the js object and the js class

  • 2020-05-09 18:08:39
  • OfStack

The code is simple, so there's no more crap.


// The first 1 Method of definition
var person=new Object(); // To create the 1 An object .
person.name="tom"; // use person Object pair call name Property, which has a value of tom
alert(person.name); // According to name Attribute values
person.say=function(){ // right person Object added 1 a say Function.
alert("person say");
};
person.say();


// The first 2 Method of definition
var person={
name:"tom",
say:function(){
alert("hello person");
}
}; // To create the 1 An object .
//alert(person.name);
//person.say();
person.age=10;
alert(person.age);
//js The defined class is used function .
var Person = function(name){ // We are defining 1 A class. As if it had 1 A constructor with arguments.
this.name =name;// Attributes of a class
this.say = function(){ // Methods of a class .
alert("say good");
}
}
var p = new Person("fox"); // define Person Of the class 1 An object p
alert(p.name); // call name attribute


Related articles: