In-depth understanding of the JavaScript constructors instance members and static member of

What is the constructor?

Constructor is a special method. Is used to initialize the object when the object is created, the object member variable that is assigned the initial value, the total used in the statement to create an object with the new operator.
As follows: Person is a constructor, and the new Person is to use a constructor function to instantiate an object ws

var Person = function(name,age){
	this.name = name;
	this.age = age;
}
var ws = new Person('王珅'5)  //实例化一个ws对象

What is a member of the constructor?

Constructor member properties and methods is the constructor
following example: name, age attribute of Person, EAT () is a method for this purpose are the constructor member

var Person = function(name,age){
	this.name = name;
	this.age = age;
	this.eat = function(){
		console.log("吃饭")
	}
}

A member function is divided into: instance members and static members

1. instance members

Definitions : By adding this member

var Person = function(name,age){
   this.name = name;   //实例成员
   this.age = age;        //实例成员
   this.eat = function(){  //实例成员
   	console.log("吃饭")
   }
}

Examples of members of the characteristic : by way of example only access objects accessed, not by the constructor; the following example

var Person = function(uname,age){
	this.uname = uname;
	this.age = age;
}
var ws = new Person("王珅",15)
console.log(ws.age) // 15
console.log(Person.age) // undefined

2. Static member

Definitions : constructor itself add members. In the following example: Person.sex, sex is the static member

var Person = function(name,age){
   this.name = name;   //实例成员
   this.age = age;        //实例成员
   this.eat = function(){  //实例成员
   	console.log("吃饭")
   }
}
Person.sex = '男’;

Examples of members of the features : the constructor only access, access can not instantiate the object; the following example

var Person = function(uname,age){
	this.uname = uname;
	this.age = age;
}
var ws = new Person("王珅",15)
Person.sex = '男’;
console.log(ws.sex) // undefined
console.log(Person.sex) // 男
Released five original articles · won praise 6 · views 70

Guess you like

Origin blog.csdn.net/qq_22841567/article/details/104596381