OOP Programming in JS
We often use the system's own objects, called system objects, such as: Array,date and so on. Variables under Objects: Properties called objects, functions below objects: methods called objects.
Create an empty object, var obj = {}; var obj = new Object ();
Factory Mode: Encapsulation function
function Createperson (name) {
1. Raw Materials
var obj = new Object ();
2. Processing
Obj.name = name;
Obj.showname = function () {
alert (this.name);
};
3. Appearances
return obj;
}
When new calls a function: This is the object that is created, and the return value of the function is directly this (implicit return)
Functions called after new: called constructors
So the factory way can be written like this:
function Createperson (name) {
THIS.name = name;
This.showname = function () {
alert (this.name);
};
}
JS Object-oriented first knowledge