JavaScript is not an object-oriented languages do?
"Object-oriented model is not only a type, prototype-based (based on the prototype) are class-based (based on the category) of the Starter Edition is a class-less object-oriented. Corresponding, prototype inheritance class are inherited Starter Edition (for example, omitted multiple inheritance, base class constructor and ignores quoted property inheritance, etc.), but not because it does not support these features will not admit it is a complete succession. whether succession to add extra features, Developers can freely select, but do not need these additional features at the time, or have reason to try to prototype-based inheritance.
All in all, prototype-based language itself may not need to think too many of reuse capability, it features a number of sacrifices to keep the language simple, there is nothing wrong with this, prototype-based than class-based easy, but it remains a real sense object-oriented. "
I. ─ ─ public and private property package
JS, the function is the "first type", JS objects and closure are achieved through the function. Use of the concept of closure, JS can be not less than that there is a variety of other object-oriented language of public and private characteristics:
function List(){
var m_elements = []; //私有成员,对象外无法访问
m_elements = Array.apply(m_elements, arguments);
//公有属性,可以通过“.”运算符或下标访问
this.length = {
valueOf : function(){return m_elements.length;},
toString : function(){return m_elements.length;}
}
this.toString = function(){
return m_elements.toString();
}
this.add = function(){
m_elements.push.apply(m_elements, arguments);
}
} This example this.length, this.toString, this.add are public members, one of this.length members are private m_elements the length of the property getter, outside we can "." Operators of such property for a visit.
Object-getter is a special kind of property, which form such as variable or object property, but its value as the incoming parameters change. At the language does not support the getter, we usually get <Name> way to replace one of <Name> are actual getter name, its effect is equivalent getter. ECMAScript v3 does not support the getter, but this structure can be used above with valueOf and toString methods to simulate the object getter.
Setter is another object corresponding to the property, its role is to adopt a similar assignment to change the object or the status of certain parameters, it is regrettable that, ECMAScript v3 does not support the setter, and so far is no good way to at JS Analog Medium setter. To achieve that only through the definition set <Name> method to achieve: (
II. The type of property and methods
In JS, the object properties and methods to support the 4 kinds of different types, the following through an example to illustrate:
function myClass(){
var p = 100; //private property; 1. 私有属性
this.x = 10; //dynamic public property 2. 动态公有属性
}
myClass.prototype.y = 20; //static public property or prototype property 3. 静态公有属性或称原型属性
myClass.z = 30; //static property 4. 静态属性或称类属性 Say the following under their characteristics and differences:
1. Private property has already been mentioned above, its characteristics are not open to the outside world only through a specific getter and setter access. Instantiated myClass () If, after the adoption of "." Operator direct access to p will be undefined;
2. Dynamic public property is characterized by the outside world can access, and each instance of an object to hold a copy of mutual influence between them will not;
3. The characteristics of the prototype property of each object instance are shared only a copy of it will rewrite the mutual influence;
4. Type of property is characterized as a type of property rather than the object instance of the property, that is not the object instance "." Operator access, as will be undefined. Example myClass.z up through direct access to.
Prototype knowledge-related points to put under a few, after all, JS object-oriented programming are the focus, content more. EASY notice about the first two notes, including the use of prototype techniques, and examples of the substance. After a few will be involved in inheritance and polymorphism, structure and destructor, packaging objects, metadata categories, such as class template.
An example of this at the end to describe briefly the prototype, "prototype in IE 4 and later versions for the introduction of a certain type of object in the method, but the special point is: it is a kind of object to add the Ways Ways ":
Number for the local object to add the number of factorial methods: Number.fact ()
Implementation:
Number.prototype.fact = function(){
var num = Math.floor(this);
if(num<0) return NaN;
else if(num==0 || num==1) return 1;
else return (num*(num-1).fact());
}
alert((10).fact()); //3628800 






