1. The object is suitable for collecting and managing data, so it is easy to form a tree-shaped structure.
JavaScript includes a prototype chain attribute that allows an object to inherit the properties of another object. Using it correctly can reduce the initialization time and memory consumption of the object.
2. Functions they are the base modular unit of JavaScript for code reuse, information hiding, and combinatorial invocation. function to specify the behavior of an object. In general, programming is the skill of breaking a set of requirements into a set of functions and data structures.
3. Modules we can use functions and closures to construct modules. A module is a function or object that provides an interface but hides the implementation state and implementation.
1. Custom type--constructor mode (pseudo-class mode)
In a class-based system, an object is defined by using a class to describe what it is. If the building is a class based system, then the architect will first draw the blueprint of the house, and then the house is built according to the blueprint.
When implementing inheritance using a custom type pattern, we simply pass parameters to the constructor and then mount the arguments on the instance object. Other methods about instance objects do not pass arguments, because this can be accessed from within the method called by the instance object. A variable that is mounted on the instance this object is called an instance variable.
Combination--Inheritance
function person (name, age, Job) {//instance variable this.name = name;
This.age = age;
This.job = job;
} Person.prototype.sayName = function () {alert (this.name);}
var person1 = new Person (' Nicholas ', ", ' Software Engineer ');
var person2 = new Person (' Greg ', N, ' doctor ');
function Supertype (name) {this.name = name;
this.colors = [' Red ', ' blue ', ' green '];
} SuperType.prototype.sayName = function () {console.log (this.name);}
Function subtype (name, age) {//Inheritance Property Supertype.call (This,name);
This.age = age;
}//Inheritance method Subtype.prototype = new Supertype ();
SubType.prototype.sayAge = function () {Console.log (this.age)} var Instance1 = new Subtype (' Nicholas ', 29);
Instance1.colors.push (' black ') console.log (instance1.colors);
Instance1.sayname ();
Instance1.sayage ();
var instance2 = new Subtype (' Greg ',) Console.log (instance2.colors);
Instance2.sayname ();
Instance2.sayage ();
On inherited and inherited methods, we call a total of two superclass constructors, and when the superclass constructor is called by new to create a subclass constructor's prototype, there is a problem that the prototype object of the subclass constructor is now an instance of the superclass constructor. So there will also be properties that are added to the superclass constructor for this instance object, except that the value is undefined, that is, when the superclass constructor function is called by new to change the prototype of the subclass builder, then there is an extra attribute on the prototype of the subclass builder. This has resulted in waste. And what we need is that the prototype of the subclass constructor can inherit the method of the Superclass builder prototype. So what we need,
1. Create a subclass Builder prototype object.
2. This subclass constructor prototype inherits from the superclass builder's prototype.
3. Since we rewrote the prototype object of the subclass constructor in 1, which is to re-create the prototype object, we need to add the constructor attribute on the newly created prototype object and assign it as a subclass constructor function.
Rewrite the code above, as shown below.
About the constructor property: A property that only exists on the stereotype of a constructor function and points to the constructor, and the overridden prototype object defaults to no constructor attribute.
Parasitic combination--inheriting
function Inheritprototype (subtype,supertype) {
var prototype = object.creat (Supertype.prototype);
Prototype.constructor = subtype;
Subtype.prototype = prototype;
};
function Supertype (name) {
this.name = name;
this.colors = [' Red ', ' blue ', ' green '];
}
SuperType.prototype.sayName = function () {
console.log (this.name);
}
Function subtype (name, age) {
//Inheritance Property
Supertype.call (this,name);
This.age = age;
}
Inheritance method
Inheritprototype (subtype,supertype);
SubType.prototype.sayAge = function () {
console.log (this.age);
}
var instance = new Subtype ();
By hiding those so-called prototype operating details, it now looks less bizarre. But is there really something to discover:
Without a private environment, all properties are public. Unable to access the method of the parent class. Difficult to debug
2. Prototype
In a pure prototyping model, we discard the class and focus instead on the object. Based on prototype inheritance, class-based inheritance is conceptually simpler: a new object can inherit the properties of an old object. You start by constructing useful objects, and then you can construct more objects similar to that object. This completely avoids the process of splitting an application into a series of nested abstract classes
In a prototype based system, we create objects that look like all of the types of objects we want, and then tell the JavaScript engine that we want more objects like this. If the building was based on prototypes, the architect would build a house and then build the house like this.
Method Object.creat () as an alternative to the new operator, uses it to create JavaScript objects, adding a more prototype-like feel.
function Mymammal = {
name: ' Herb ' mammal ',
get_name:function () {return
this.name;
},
says:f Unction () {return
this.saying | | '';
}
}
var mycat = object.create (mymammal);
Mycat.name = ' Henrietta ';
mycat.saying = ' Meow ';
Mycat.purr = function (n) {
var i, s = ';
for (i = 0;i < n; i + 1) {
if (s) {
s = = '-'
}
S + = ' R ';
}
return s;
}
Mycat.get_name = function () {return
this.says + ' + this.name + this.says;
}
This is a "differentiated inheritance". By customizing a new object, we indicate the difference between it and the base object on which it is based.
Sometimes it is useful for situations where some data structures inherit from other data structures.
3. Functional-Factory mode
In pseudo class mode, the constructor function cat has to repeat the work already done by the constructor mammal. That is no longer needed in a functional pattern, because the constructor cat will invoke the constructor mammal, allowing mammal to do most of the work in object creation, all cat focus on its own differences.
The functional pattern has great flexibility. It not only brings less work than pseudo-class patterns, but also gives us better encapsulation and information hiding, as well as the ability to access parent-class methods.
If we use a functional style to create an object, and all the methods of that object do not use this or that, then the object is persistent. A persistent object is a collection of simple functional functions.
Private variable: Any variable defined in a function can be considered a private variable because it cannot be accessed outside the function.
Closed Bag
Closures are methods that prevent the garbage collector from removing a variable from memory so that it can be accessed outside the execution environment in which the variable is created.
Keep in mind that closures are created by functions. Each call to the function creates a unique execution environment object. When the function finishes executing, the execution object is discarded unless the caller references it. Of course, if a function returns a number, it cannot refer to the execution Environment object of the function. But if the function returns a more complex structure, such as a function, object, or array, and the return value is saved to a variable, a reference to the execution environment is created.
Function.prototype.method = function (Name,func) {this.prototype[name] = func;
return this;
}//Factory mammal var mammal = function (spec) {var that = {};
That.get_name = function () {return spec.name; } that.says = function (spec) {return Spec.saying | |
'';
return to that; }//Factory Cat function (based on mammal function) var cat = function (spec) {spec.saying = Spec.saying | |
' Meow ';
var that = mammal (spec);
That.purr = function (n) {var i, s = ';
for (i = 0; i < n; i + + 1) {if (s) {s = = '-';
} s + = ' r ';
} that.get_name = function () {return that.says () + ' + spec.name + ' + that.says ();
return to that;
///Create Mycat object var Mycat = Cat ({name: ' Henrietta '});
Object.Method (' Superior ', function (name) {var = this, method = That[name]; return function () {return method.apply (this, arguments)}})//Factory Coolcat function (based on cat function) var Coolcat = function (spec {var that = Cat (spec), Super_get_namE = That.superior (' get_name ');
That.get_name = function (n) {return ' like ' + super_get_name () + ' baby ';
return to that;
var mycoolcat = Coolcat ({name: ' Bix '});
var name = Mycoolcat.get_name ();
The functional module pattern has great flexibility. It does not only bring less work than constructor patterns, but also gives us the ability to better encapsulate rest and hide, and to access the methods of the parent class. If all states of an object are private, the object becomes an "Tamper-proof" object. The object's properties can be replaced or deleted when the object's integrity is not corrupted. We create an object with a functional style, and all the methods of that object do not use this or that, then the object is a persistent object. A persistent object is a collection of simple function functions.
A persistent object is not invaded. When accessing a persistent object, the attacker does not access the internal state of the object unless a method is authorized.
Module mode
The preceding pattern is used for custom types to create private variables and privileged methods. Douglas's modular model, however, is to create private variables and privileged methods for the single example. A single example refers to an object that has only one instance. (That is, an object created with the object literal notation)
var singleton = function () {
//private variables and functions
var privatevariable = ten;
function Privatefunction () {return
false;
}
Privileges/Public methods and properties return
{
publicprovperty:true;
Publicmethod:function () {
privatevariable++;
return privatefunction ();}}
Essentially, this object literal defines a single instance of the public interface. This pattern is useful when you need to initialize some of the individual cases while maintaining their private variables. In short, if you have to create an object and initialize it with some data, you also expose some ways to access the private data.
Enhanced Module mode
This enhanced modular pattern is appropriate for instances where the singleton must be of a certain type, and some attributes and methods must be added to enhance it.
var singleton = function () {
//private variables and functions
var privatevariable = ten;
function Privatefunction () {return
false
}
//Create object
var object = new Customtype ();
Add privileges/public properties and methods
Object.publicproperty = true;
Object.publicmethod = function () {
privatevariable++;
return privatefunction ();
}
return object;
} ()