The context dependency of the object
varstr ="I am a String object, I declare here, but I do not exist independently! " varobj = {des:"I am an object, I declare here, I do not exist independently. " }; varFun =function () {Console.log ("I am a Function Object! Who calls me, who I belong to:", This ); }; Obj.fun=Fun ; Console.log ( This= = = window);//Print TrueConsole.log (window.str = = = str);//Print TrueConsole.log (window.obj = = = obj);//Print TrueConsole.log (Window.fun = = = Fun);//Print TrueFun ();//Print I am a Function Object! Who calls me, who I belong to: WindowObj.fun ();//Print I am a Function Object! Who calls me, who I belong to: objFun.apply (str);//Print I am a Function Object! Who calls me, who I belong to: Str
Literal (literal notation) object declaration
var person = { name: "Zhang San", Gender: "Male", eat:function (stuff) { c13/>+ stuff); } ; 176 ; Delete person["age"];
Creating an object using the constructor (constructor)
// constructor The person itself is a function object function Person () { // can do some initialization work here } // It has a property called prototype Person.prototype = { name: "Zhang San", +, Gender: "Male", eat:function (stuff) { + stuff); } } // constructing an object using the New keyword var New Person ();
__proto__ properties and implicit references for objects
function Person (name) { This. Name =name; } varp =NewPerson (); //An implicit reference to an object points to the prototype property of the constructor, so printing true hereConsole.log (p.__proto__ = = =Person.prototype); //The prototype itself is an object, so his implicit reference points to the//the prototype property of the Object constructor, and therefore prints trueConsole.log (person.prototype.__proto__ = = =Object.prototype); //The constructor person is itself a function object, so printing true hereConsole.log (person.__proto__ = = = Function.prototype);
Using prototype chain Horse->mammal->animal to implement inheritance
//declaring the Animal object constructor functionAnimal () {}//point the Prototype property of Animal to an object, //can also be directly understood as the prototype of the specified Animal objectAnimal.prototype ={name:animal", weight:0, Eat:function () {alert ("Animal is eating!" ); }}//Declare Mammal object constructor function mammal () {this.name ="Mammal"; }//Specifies that the mammal object is prototyped as a Animal object. This is actually the prototype chain between the creation of the mammal object and the Animal object mammal.prototype = new Animal (); Declares the Horse object constructor function Horse (height, weight) {this.name ="Horse"; This.height = height; This.weight = weight; }//Specify the prototype of the Horse object as a Mamal object and continue to build the prototype chain between Horse and mammal Horse.prototype = new mammal (); Re-Specify the Eat method, which overrides the Eat method inherited from the Animal prototype Horse.prototype.eat = function () {alert ("Horse is eating grass!" ); }//Verify and understand the prototype chain var horse = new Horse (100, 300); Console.log (horse.__proto__ = = = Horse.prototype); Console.log (horse.prototype.__proto__ = = = Mammal.prototype); Console.log (mammal.prototype.__proto__ = = = Animal.prototype);
Using simple inheritance to implement class-like inheritance
//declaring the person class varperson =class.extend ({_issleeping:true, Init:function(name) { This. _name =name; }, Issleeping:function() { return This. _issleeping; } } ); //declare the Programmer class and inherit the person varProgrammer =person.extend ({init:function(name, issleeping) {//calling the parent class constructor This. _super (name); //set your own State This. _issleeping =issleeping; } } ); varperson =NewPerson ("Zhang San" ); varDiors =NewProgrammer ("Zhangjiang male",false ); //Print TrueConsole.log (person.issleeping ()); //Print FalseConsole.log (diors.issleeping ()); //true here, so print trueConsole.log (personinstanceofPerson && PersoninstanceofClass&& DiorsinstanceofProgrammer &&diorsinstanceofPerson && diorsinstanceofClass);
Using closures for information hiding
//declaring the User constructor functionUser (pwd) {//Defining private Properties varPassword =pwd; //Defining Private Methods functionGetPassword () {//returns the password in the closure returnpassword; } //privileged function declaration, which is used by other public methods of the object to access private members through this privileged method This. Passwordservice =function() { returnGetPassword (); } } //Public member DeclarationUser.prototype.checkPassword =function(pwd) {return This. Passwordservice () = = =pwd; }; //verifying the hidden nature varU =NewUser ("123456" ); //Print TrueConsole.log (U.checkpassword ("123456" ) ); //Print undefinedConsole.log (U.password); //Print TrueConsole.log (typeofU.gepassword = = = "undefined");
Comprehensive understanding of object-oriented JavaScript
Comprehensive understanding of object-oriented JavaScript