JavaScript kernel note 07-object-oriented, javascript07-
JavaScript isObject-basedBut the features of JavaScript functional language make it itselfProgrammableIt can become any form you want. JavaScript OO-style code development:
1. Prototype inheritance
Inheritance in JavaScript can be implemented using prototype chains. A method on an object is called because a method is referenced by another function object in a JavaScript Object, therefore, the interpreter searches for this attribute in the object.
If this parameter is found, search for the prototype object on the internal object prototype object. Because the prototype object and the object itself have the same structure, this process will be traced back to the discovery of this attribute, this attribute is called. Otherwise, an error is reported.
Prototype inheritance:
// Prototype inherits function Base () {this. baseFunc = function () {alert ("base behavior") ;}} function Middle () {this. middleFunc = function () {alert ("middle behavior") ;}} Middle. prototype = new Base (); // inherit Basefunction Final () {this. finalFunc = function () {alert ("final behavior") ;}} Final. prototype = new Middle (); // inherit Middlefunction test () {var obj = new Final (); obj. baseFunc (); obj. middleFunc (); obj. finalFunc ();} test ();
2. Reference
The difference between JavaScript applications and other languages is that its reference always points to the final object rather than the reference itself, as shown in the following example:
// Reference the instance var obj ={}; // empty object var ref = obj; obj. name = "objectA"; alert (ref. name); // ref also adds the name attribute obj = ["one", "two", "three", "four"]; // obj points to another object (array object) alert (ref. name); // ref also points to the original object alert (obj. length); // 3 alert (ref. length); // undefined // Description: <span style = "font-size: 18px; color: # ff0000; "> <strong> obj is only a reference to an anonymous object </strong> </span>. Therefore, ref does not point to it. When obj points to another array object, you can see that, reference ref has not changed. // Always point to the "null" object that is added with the name attribute {}.. <Strong> modifying the direction of obj does not affect ref, but modifying the value of the original object affects the referenced object. </strong>.
3. new operator
Function objects can be defined using the new operator to create objects similar to java. For objects in json format, new cannot be used for creation, and function () {} can be defined by default.
For example:
Var a = {name: 'name'}; var B = new a (); not feasible
Var a = function () {alert ('3') ;}; var B = new a (); feasible
4. Encapsulation
In fact, we can implement encapsulation through JavaScript functions. The advantage of encapsulation is that unauthorized Customer Code cannot access data that we do not disclose, as shown in the following example:
// Encapsulation, a common and private defined function Person (name) {// Private variable privatevar address = "China"; // public method publicthis. getAddress = function () {return address;} // common variable publicthis. name = name;} // The prototype Extension Method of the prototype. The total method is Person. prototype. getName = function () {return this. name;} Person. prototype. setName = function (name) {this. name = name ;}
<Strong> Person. AGE = 1000; // static variable </strong>/* Description: first, declare a function as a template. In object-oriented terms, it is a "class ", the variables declared in var mode are only visible inside the class, so address is a private member variable. The only way to access address is by using the exposed getAddress method, the set/getName method is the method on the prototype chain </strong>, so it is public. Test: */var jay = new Person ("jay"); alert (jay. name); // accessible, return: jayalert (jay. getName (); // accessible, return: jayalert (jay. address); // inaccessible, return: undefinedalert (jay. getAddress (); // accessible, return: China
<Strong> alert (Person. AGE); // access by name </strong>