According to JavaScript founder Brandon Eich, the best language constructs for JavaScript are:
- function is first class citizen (first functions)
- Closures (closure)
- Prototypes
- OBJ = {p1:1, P2: ' ABC '}
- Array = [+ +]
I think that JS's most brilliant idea is to break through the limitations of OOP at that time, to turn object instead of class into a first citizen (Eich's 1,4,5 Three is the manifestation of this idea).
C++,java,c# and other languages are in fact class-oriented, emphasizing type, not as an individual support of object, only "commonality" and not "personality". JavaScript, as a "dynamic language", naturally supports the dynamic creation of object, which supports the dynamic modification of object.
Here are my additions to Eich:
JavaScript object is a class citizen, the function is the super-set of object
The function is a normal object, it has a property, or it can have "member function", the following code can prove this:
function foo(x, y)
{
if (foo. xyz = = = undefined) {
foo. xyz = 0;
}
else {
foo. xyz + = x + y;
}
Console.log (' foo. XYZ: ' + foo. xyz ');
}
Foo (1, 2); 0
Foo (1, 2); 3
Foo (1, 2); 6
The above function Foo defines property xyz, which is the same as the normal object.
Function can also have "member function", the following code can prove this:
function foo(x, y)
{
var z = (x + y);
Console.log (' x + y = ' + Z);
}
Foo . Bar = function () {Console.log (' Hello, I am Bar. ')};
Foo (1, 2); X + y = 3
Foo . Bar ();//Hello, I am bar.
But a non-function object cannot be used as a function, and the following code does not work:
var obj = {p1:100};
obj ();
The conclusion is that the function is a normal object, not a special citizen, it is nothing more than a ()operator. The () operator is a property that contains the executable string (executable literal).
JavaScript support for the class concept
It says that JavaScript really raises the status of object to the top, expands and enriches our understanding of OOP.
However, class is still indispensable and even critical to any serious OOP project. This is the third article of Eich: the importance of prototype. In the process of consulting, I often help not to use class as a project to rationalize the class and class inheritance, greatly reduce duplication of code, and improve the abstraction and maintainability of the program.
JavaScript has no class. Microsoft's typescript plus class,interface, and inheritance support, are implemented through functions and prototype. Prototype, together with the object of a class citizen, is the golden key to the realization of classes.
So what is prototype? in fact, it's nothing more than a function object. the named ( named) property. just this attribute is attached to the language itself, not programming plus. It is the "commonality" of functions, and we know that class is the common denominator of object.
Let's take a look at how to use a function (particularly called constructor), and prototype to simulate class
function MyClass(x, y)
{
THIS.PROP1 = x;
This.prop2 = y;
}
MyClass. prototype. GETPROP1 = function () {Console.log (' this.prop1: ' + THIS.PROP1);}
var obj = {}; line1: Manually Create an Object
Myclass.call (obj, 10, 20); line2: Call constructor manually on Obj1
Obj.constructor = MyClass; line3: Set MyClass as obj ' s constructor manually
Callmemberfunc (obj, ' getProp1 '); line4: Ten (simulating member functioncall)
var obj2 = {}; line5: Manually Create another object
Myclass.call (Obj2, 30, 40); Line6: Call constructor manually on Obj2
Obj2.constructor = MyClass; Line7: Set MyClass as obj ' s constructor manually
Callmemberfunc (obj2, ' getProp1 '); line8: (simulating member functioncall)
Each function of JavaScript has a prototype property. In fact, this is only convenient, create a property of their own as well. The purpose of this property is to add properties and functions to any object of this function that is similar to a C + + vtble table. So, how do you hook an object with this prototype? JavaScript gives each object a new attribute to be a member of class:constructor. constructor as an ID to identify a particular class, and this constructor, of course, is not responsible for initializing the function of the!
All of this can be done manually using the existing language constructs provided by JavaScript. This is illustrated in the example above.
Our goal is to create MyClass 's instances: obj and obj2. MyClass is a "constructor" or, more specifically, an "initialization function".
We first manually create obj (line1), then call MyClass (line2) to initialize the new object, and then the constructor of obj the attribute is set to MyClass (line3) so that the prototype of obj and MyClass are contacted.
We created the Obj2 (line5 ~ line7) in the same way.
Does the two object belong to the same class? Yes, there are two proofs below:
a) Obj.constructor = = Obj2.constructor was established, proving that they belong to one of the same constructors, namely the same genus.
B) Callmemberfunc can call any function with Myclass.prototype (line4 and Line8)
So what is Callmemberfunc, is it universal? The answer is yes, here is my callmemberfunc definition, which can be used to call any function (need to enhance some functions, such as function parameter processing).
Call a member function for an object
function Callmemberfunc (obj, name) {
if (Obj[name]!== undefined) {
Console.log (' getprop ' + ' found ' + name + ' value: ');
return Obj[name].call (obj);
}
if (obj.constructor!== undefined) {
Console.log (' getprop ' + ' found ' + name + ' in Constructor.prototype, Value: ');
return obj.constructor. prototype [Name].call (obj);
}
}
In short, JavaScript can be implemented by simply constructing the class-oriented program, which enables the class function of OOP. I'm just a partial implementation here, but the basic functionality is consistent with the code generated by JavaScript compilation.
JavaScript's syntax sugar also makes it easy to implement these ideas:
var obj3 = New MyClass (40, 50); line9: Use ' new ' operator
OBJ3.GETPROP1 (); Line10: 40
JavaScript's new operator (line9) replaces the manual code above me (line1 ~ line3), and Callmemberfunc is a simple "direct call "(line10) replaced.
Summarize
JavaScript programming is a new experience and extension of OOP, which really refers to the lofty status of object, and is a great unification of FP (functional programming) and OOP on the basis of OOP.
2014-9-1 in Seattle
JavaScript function Analysis of OOP