This article is a good part of JavaScript about objects and inherited learning notes.
1. Object.create
This function is a standard function in ECMAScript 5, which uses an object as a prototype to generate another object, which can be implemented with the following code simulation.
if(typeofObject.create!== ' function ') {object.create=function(proto) {varF =function(){}; if(typeofProto!== ' object '){ //Follow the chrome error pattern. Throw NewTypeError (' object prototype May is only is an object or null: ' +proto); } F.prototype=Proto; return NewF (); }; }
The idea is to use an internal function as a delegate to isolate the new object that is needed from the original object.
About error handling is generated based on Chrome's current error message.
2. New
New is a keyword inside JavaScript, its specific role, please refer to MDN. The following code can be used for simulation.
function (constructor, args) { var that = object.create (constructor.prototype), = Constructor.apply (that, args); return (typeof(Other) = = = ' object ' && other) | | that ; }
3. Inheritance
3.1 Pseudo-Traditional way (pseudoclassical)
The book does not recommend this way of simulating traditional inheritance. Because it deviates from the nature of the JavaScript stereotype inheritance.
3.2 Prototypes (Prototypal)
The sample code is shown below.
varMymammal ={name:' Herb the mammal ', Get_name:function ( ) { return This. Name; }, says:function ( ) { return This. Saying | | ‘‘; }};varMycat =object.create (mymammal); Mycat.name= ' Henrietta '; mycat.saying= ' Meow '; Mycat.purr=function(n) {varI, S = "; for(i = 0; i < n; i + = 1) { if(s) {s+ = '-'; } s+ = ' r '; } returns;}; Mycat.get_name=function ( ) { return This. says () + "+ This. Name + "+ This. says ();};
Subclasses do not have properties and methods in the parent class constructor, but private variables in the type are not protected.
3.3 Function (functional)
var function (spec, my) { varotherPrivate instance variables= My | | {}; ADD shared variables and functions to my New Object; Add Privileged Methods to that return that ;};
The bold place needs to be replaced by the actual code.
The place to be explained is as follows.
- Spec is the information required to construct an object.
- My is information that the constructor can share with other objects, not required.
- Constructing an object (a new object) can be literal, using the new construct, using object.create, calling other functions that return the object.
- Private properties inside the function are not visible externally.
- The methods for adding private functions are as follows.
var function () { = methodical;
For example, when we call methodical, we use the internal methodical instead of the that.methodical, which prevents this function from being tampered with.
The examples in the book are as follows.
Vvar mammal =function(spec) {varthat = {}; That.get_name=function ( ) { returnSpec.name; }; That.says=function ( ) { returnspec.saying | | ‘‘; }; returnthat ;};varMymammal = Mammal ({name: ' Herb ')}); varCat =function(spec) {spec.saying= Spec.saying | | ' Meow '; varthat =mammal (spec); That.purr=function(n) {varI, S = "; for(i = 0; i < n; i + = 1) { if(s) {s+ = '-'; } s+ = ' r '; } returns; }; That.get_name=function ( ) { returnThat.says () + "+ spec.name +" +that.says (); } returnthat ;};varMycat = Cat ({name: ' Henrietta '});
JavaScript Learning Notes-objects and inheritance