Javascirpt is a quite different language than C #, and C # is a Class-oriented static programming language (here we do not consider the dynamic features added by C #4.0 ), javascript is an object-oriented functional programming language.
Although C # is very different from javascript, we may also use the skills we have mastered to speed up new skills.
1. javascirpt object-oriented features.
C # Is a facial image class. It uses a two-step method to obtain an object that can be operated. First, define a class using the class keyword, and then obtain an object using the new keyword. This includes the following layers.
1. First, we need to define the structure of an object, which is a class
2. We use this structure to assign the State to obtain an object (or instance object)
3. When the new keyword is used, the system calls a constructor to assign an initial state to the object.
Conclusion: C # determines the object structure first, and then the object state. It is not allowed to change the object structure after the object State is determined. The so-called "two-step method" is the first structure, followed by the state. The structure cannot be changed when the status is available.
Javascript can adopt the "one-step method", that is, the operation that determines the structure and status is completed in the same step.
For example, cat. color = "while", this operation adds a color structure to the object, while assigning the property "white" (white. The above operation is used anywhere in the program.
Because the "one-step method" is used, the existence of the class is meaningless. As long as we have an object, we can add both structures and States to the object anytime and anywhere, or assign values to attributes and values. (If you want to stick to the concept of a class, the classes in javasrt rt are constantly changing)
In C #, The new Keyword is the only way to generate an object. In javascript, The new Keyword is only one of the two ways to generate an object.
The first way to generate an object is to use the "{}" operator.
Baby = {};
Singer = {name: "Zhang Xueyou", sex: "male"}
1st objects have no structure, and 2nd objects are famous for being sexual. You can add other attributes later.
At this point, you may ask why the new Keyword is still needed with the "{}" operator that easily generates objects?
Using the new keyword, you can call a function (constructor or constructor) to complete the action of building an object, so as to obtain the effect of the "2-step method.
For example
Function Person (name, sex ){
This. name = name;
This. sex = sex;
}
Singer = new Person ("Zhang Xueyou", "male ")
Although it looks like c #, it seems that the "2-step" is adopted, the Person function defines the structure, and the new statement gives the State. But this "two-step method" is different from the "two-step method" of C #, because its "two-step" is not very strict "two-step ".
If we comment out the statements in the constructor, replace it:
Function Person (name, sex ){
// This. name = name;
// This. sex = sex;
}
Singer = new Persion ()
Singer. name = "Zhang Xueyou"
Singer. sex = "male"
The singer object obtained at the end is exactly the same as the singer object obtained by the handler.
We can freely remove the first step of defining the structure from the constructor and still use the "1-step method" to obtain the equivalent effect.
Because javascript can add structures and States at any time through the "1-step method", "{}" or "new" is used to generate new objects, you can freely choose based on your actual needs (very free ).
2. Inheritance of javascript.
Because the construction of javascript objects adopts the "one-step method" and the structure and state are added at the same time, the inheritance of javascript is also "synchronous", and the inheritance structure and State are also inherited.
In C #, It is inherited through a class, and only inherits the structure, without inheriting the state. This inheritance is implemented through the parent class. In javascript, the structure and state must be inherited at the same time, so the object is inherited.
Therefore, C # is a Class-oriented language, while javascript is an object-oriented language (inheriting both the structure and status ).
A more standard statement is that javascript uses prototype to implement inheritance. Prototype is the parent object (corresponding to the parent class in C ). The sub-object inherits all the structures and states of the parent object (provided that the structure and state of the parent object do not exist)
There is a learning difficulty here, that is, an object is indirectly connected to its parent object instead of directly connecting to its parent object through the prototype object of the constructor attribute.
The Code is as follows:
Function Monkey (){
}
Function Person (){
}
Singer = new Person ();
Person. prototype = new Monkey ()
Person. prototype. constructor = Person
// Assert_true singer. constructor = Person, that is, the singer object has a constructor attribute pointing to Person, and the Person points to the new Money () object through the prototype attribute.
People who like to explain why a child object cannot directly direct a prototype attribute to the parent object?
This is actually possible. For example, in Firefox, each object has a _ proto _ attribute pointing to its parent object. (However, this attribute is not recommended for Users)
Why don't other browsers do this? Constructor (constructor) is used to indirectly connect to the parent object. In fact, this is for a good reason and is a well-designed design.
If each object can be directly connected to a parent object, a very confusing situation may occur. If we add attributes and States to the parent object at any time through the "1-step method, the quilt object is inherited at any time. If an error occurs, we need to check the entire program to find the trouble. This is obviously not a good design.
So what should we do?
Through the above example, we know that through the constructor, we can get the two-step effect, so we connect the parent object to the prototype attribute of the sub-object constructor, and define a "gentleman agreement". All attributes that need to be added are placed in the constructor. In this way, we only need to check the constructor to know which structures the sub-object inherits (note, state inheritance is unrestricted, because the constructor is the first step in the two-step method and determines the structure ).
Given that prototype assignment must rely on constructor, it is "audible and inductive" that allows you to "naturally" use the "2-step method" to complete inheritance, better code organization. However, because it is a "gentleman's agreement", if you add a structure outside the constructor, it will also be completely done, but it may cause some size problems in some cases.
Some may ask, if all objects are well understood and pulled out functions, they are confused. In fact, functions are objects. It would be hard to understand this fact. We can try the following statement.
There are no functions in the world. They are all objects. However, an object has a special property "()". To use this property, you must omit ". "This operator, for example," Person () "rather than" Person. () ", this property is not like singer. the name attribute directly outputs a State, such as "Zhang Xueyou". Instead, it can use a piece of code hidden inside it to complete a computation and add some parameters in, this will influence the running of its internal code. Because it is an object, other objects can also appear. Finally, we call this object a function. It also makes functional programming possible.
So far, have you noticed that javascript object-oriented is so elegant, flexible, and concise? The seemingly obscure prototype design actually contains the painstaking efforts of the designer.
Functional Programming in javascript has brought this beauty to the extreme, which is simply amazing. (In other words ).
In addition, by learning javascript, you can have a better understanding of the static features of c. It also helps to further learn the dynamic features added to c #4.0 (although I am not optimistic about the dynamic features of c #, the main reason is that static and dynamic features are included at the same time, but it is difficult for programmers to choose. This is why Java has been resisting dynamic features ).
By helping others