Home > Web Front-end > JS Tutorial > Summary of methods for creating js objects and js classes_javascript skills

Summary of methods for creating js objects and js classes_javascript skills

WBOY
Release: 2016-05-16 16:24:51
Original
1053 people have browsed it

The code is very simple, no more nonsense.

Copy code The code is as follows:

//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();

Copy code The code is as follows:

//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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template