Recently learned JS object-oriented, in order to allow themselves to better understand, this blog as a deepening of their learning impressions of the summary (there may be a lot of shortcomings, welcome correction).
JS through the function to create the object, and JS itself is an object, then what is the object, the object contains two kinds: 1, attributes (static characteristics) 2, methods (dynamic characteristics).
To create an object by using JS:
2. Use this keyword to set properties and methods
function student () {
THIS.name//student contains the name attribute
This.sex//student contains the gender attributes
This.show=function () {
Console.log (this.name); The actions contained in the student
}
}
Call: Var zhangsan=new student ();//zhangsan to call this student function
Zhangsan.name= "Zhangsan";//Enter the name as "Zhangsan"
zhangsan.sex= "male";//Enter gender as "male"
Zhangsan.show ();//Invoke the action contained in student (the name of the student with the console output)
3. Set Object (objects)
function student (object ()) {
Var o=new object ();
O.name=name;
O.nsex=sex;
O.show=function () {
Console.log (this.name); The actions contained in the student
};
return o;
}
JavaScript Object-Oriented basics