This article assumes that the reader is familiar with the C/C ++ language. If you are not familiar with the C/C ++ language, you can ignore the discussion in the C/C ++ section and simply read the Javascript section, this article provides some knowledge points for the author to learn about the Javascript language.
Javascript has always been intended for objects, including functions. we can assign a function value to the object, so it becomes a function that can be called for execution. however, in fact, a function is just a pointer. A JavaScript Object can only accept a function pointer, which is also a feature of the C/C ++ language. generally, a function has only one copy in the memory, even for JavaScript.
Javascript is an object. In fact, the variables in the script language are non-typed, which is an illusion that C/C ++ also has the variant type and can be assigned values of various types, javascript only has this type preset in the language. This type can save different types of data. it turns out that even if we give up the C ++ free custom type method, we can still do the same thing, just like JavaScript. using one or several pre-fabricated types, we can fully construct various data structures,
Even for C ++ class definitions, the final types are the predefined types, and complex types are the combinations of simple types. javascript reduces the number of pre-fabricated types to a very small value, including strings, numbers, functions, and objects. You generally don't have to worry about which type it is. The most important thing is that, you do not have to make it inconvenient to assign values to different types of variables like C/C ++. javascript can automatically convert most types of variables.
Javascript variables can be used without declaration, but this is a misunderstanding. Variables in any script language must be declared. Instead of declaration, it is accurate to declare a variable using a value assignment statement. in addition, many times, we do need to declare a variable, but leave it for later assignment. Therefore, it is inconvenient to use a scripting language that does not declare keywords, you may have to assign an unnecessary value to a variable to declare it. variables that are neither declared nor assigned values will prompt undefined syntax errors if used directly.
Important: javascript uses the VaR keyword to declare a variable. If a local variable does not use the VaR declaration, it must not have the same name as the global variable, otherwise, it is the global variable instead of a new local variable. this is normal. The same is true for C/C ++, but JavaScript does not require variable declaration. programmers may want a local variable, but it is exactly the same as the global variable. c/C ++ errors do not occur,
Because you must declare a local variable first. of course, another rule causes the programmer to almost add a var declaration to the variables in the function, because a variable that does not use the VaR declaration, even if it appears only inside the function body, it is actually a global variable and can be accessed anywhere. if you do not want the variables in the function to be used elsewhere, or add the VaR keyword, otherwise many functions do not know which variable may have the same name and are considered to be the same variable, in the process of function execution, various strange problems occur (after a function is called, how does one of my Variables change? The answer is that the called function may have a variable with the same name, and VAR declaration is not used ).
Javascript is a dynamic scripting language. The true meaning of this sentence is that JavaScript variables are created only when they are executed. This is different from C/C ++ statements, except for new objects, all declared variable objects in C/C ++ exist in the memory at the beginning, including local variables. Of course, they may not be initialized, the memory space is reserved, and local variables even use the memory space in the stack. there are also exceptions. For JavaScript Functions
Funcname () {} is declared in such a way that these function objects have been created before any statement is executed. however, only in this way, if you use funcname = function () {} to define a function, you must execute this sentence before the function can be created.
Javascript has an important basic type object, which is the basic type of all object types, but a value type variable is not an object. You can assign a value to a variable in the following way:
// Integer var I = 0; // string var STR = "ABC"; // array var A = ["A", "B", "C"]; // object var OBJ ={}; // assign the initial value var OBJ = {name: "myobject" to the object; FUNC: function () {alert ("func call ");}};
Up to now, you can only use the value assignment statement, that is, "=" to initialize a variable. this is no problem, but sometimes it is inconvenient. The New Keyword of C/C ++ and the member function calls of the class provide convenience, in many cases, C can simulate class member function calls of C ++. You only need to assign a specific value to this pointer when saving an object pointer globally and calling a function, then all functions are called for this object. In fact, C ++ implements this, But It automates these things. c ++ uses ECx to save the this pointer,
It is equivalent to an invisible parameter added to each member function. You only need to write the member function call of the object, and the object pointer will be automatically passed to that function.
Javascript also provides this convenience. The core of JavaScript is function calling. Everything is a function. We don't need any class. Javascript also has this mechanism. Who is this? Who calls this function, for example: obj. func (); this is the OBJ In the func function. If not, what is the call? Define a function func and call it. In this case, this in func is a default value, and it is a global window object on the webpage;
On other platforms, it also corresponds to an object, or null. that is to say, if a function is called directly instead of writing to a member of an object, do not abuse this because this is a global default object, if you really want to access it, you do not need to use the this keyword. in short, this in the function depends on who calls it.
Javascript's new JavaScript generally uses such a statement to create an object: var OBJ = new myobject (); here myobject is a function. this statement is used to create an object and call the myobject function with this object. When the myobject is called, this internally accesses the newly created object,
After the function is executed, the newly created object is returned. we will find that, even if the new keyword is not used, if we define the myobject function to create an object internally and perform some operations on it, then this value is returned, as shown in the following code: vaR OBJ = myobject (); the effect is the same. functions provided by the system, such as array and object, can be called using new or not using new. The functions are the same, but this is limited to system functions and functions defined by ourselves, the effects of these two methods are different. note: If you do not want to use new to create an object, the function should create it by itself, and you do not need to use the keyword this.
The two functions are written differently.
Because we can add any attribute to an object, an object can be considered as a class. however, the definition of this class is dynamic, rather than being pre-defined and instantiated as C ++. in fact, C ++ can also write a class, and then provide a method for dynamically adding data and functions (pointers) to this class, but the access method cannot be used ". "or"-> ", but a function. another point is that the object we want to create will be initialized at one time, instead of creating the object first, and then calling a function to initialize it.
The New Keyword can achieve this effect. define a function first, write the code of the object to be initialized in this function (use this to access the object to be created in the future), and then write as long: vaR OBJ = new myobject (Param ...); it completes the creation and initialization of OBJ, which is very "like" C ++ class.
The difference between the value type and the object type is not only to specify who is the value type, who is the object type, the value type and the object type have the classification standard. we can think that every variable in Javascript is a pointer that points to an object or does not point to any object (its value is null at this time). Is this true? It is not very important, we don't care about how to implement the underlying layer. We only need to know this logic. In fact, you can also regard the null variable as pointing to a null object.
The Value Type points to a constant object. What is a constant object? In general, constant objects are non-attribute objects. the javascript language only provides operations to change the attributes of an object. If an object has no attributes, its values cannot be changed. note: The object is not a variable. A variable is just a pointer and assigned a value to it, that is, to point it to a new object. object is an object with attributes.
In fact, it is necessary to distinguish between variables and objects pointed to by variables, because you cannot assign or change attributes to values, so the only thing you can do is reference this object or point the variable back to an object. This will make the value type look like it has its own value. A string is a value type object, so you can never change a string. What you can do is create a new string. another point is that an object type means it has no value, and all its content is in the attribute. however, JavaScript
The built-in object has some additional things. For example, if a function can be executed, it must have an internal structure outside of the object. However, we cannot access these additional content using attributes. in addition, remember that all variables are the same. The so-called value type variables and object type variables refer to the types of objects they direct. therefore, JavaScript has never been copied, unless a function is called explicitly, any operation to assign values to a variable simply points the variable to a new object (here, the null value is interpreted as pointing to a null object) without other complicated operations.
Remember: javascript variables never have any values. All JavaScript variables are simple pointers. When we use "=" to assign values to a variable, is to point this variable pointer to a new object, rather than changing its original content to the object.
How does one inherit JavaScript to complete the inheritance function? Inheritance is a written class (a function for Javascript). We want to define another class, which has the functions and data members of the class we want to inherit. javascript introduces the prototype attribute, which is unique to function objects,
The object does not exist. It makes no sense even if it is added. although a function is also an object, there are two different aspects of the function. One is that it can be called, and the other is that the prototype attribute of the function becomes the reference property of the inherited function during the new operation. each function has the default prototype attribute, but the initial value is an empty object. The prototype attribute is a common object. Any object can be set to the prototype of a function, however, numeric types, such as numbers and strings, do not work because the numeric type only has values and has no attributes. Prototype
It only cares about its attributes and does not care about its values. The values have no meaning for prototype. the property of a JavaScript Object is actually a member of a C ++ object. When a javascript object is created using a function and a new function, the prototype attribute (member) of the function is ), automatically becomes the property of the newly created object. for example, the following code:
function A(){this.Name = "A";this.Call = function(){alert("A Call");}}function B(){this.Value = 0;this.Func = function(){alert("B Func");}}B.prototype = new A();var b = new B();for(var i in b){alert(">"+i+":"+b[i]);}
Run the code. We can see that B has four attributes: value, func, name, and call. the nature of inheritance is that B automatically adds B. attributes of prototype. Here, only variables are added, that is, four variables exist in B, but these variables are not new objects, either the B function is added to it, or B. the object to which the property with the same name as prototype points. if the property created by function B is exactly the same
B. A prototype attribute has the same name. attribute name) access the attribute added by B. of course, this just seems like this. In fact, the true operation is that object B only saves a B. the prototype pointer does not create those attributes, that is, B "remembers" B. prototype: When you access an attribute, if B does not exist, the interpreter will find it in the object to which the pointer is saved, B "has" This attribute. if we assign a value to this attribute of B, this attribute will be created at this time. Of course, this assignment only changes
B points to its own property, while B. the property pointing and pointing objects in prototype have not changed. B "remembers" B. prototype is because even if B has been created, we have changed B. prototype will still "perceive", that is, the inherited attributes of B. If it is not overwritten, it will also change. Of course, this will happen.
The prototype chain stores a prototype pointer for each object, but this pointer is invisible. since this pointer points to an object, the object will also have its own prototype pointer until it is null. in fact, this is the most primitive new object () object
The prototype pointer is null. any prototype pointer attribute on this chain can be read as its parent object attribute, but if it is written, a new attribute will be created, without changing the properties of the prototype. (In Some browsers, the hidden prototype attribute can be accessed through the _ PROTO _ attribute name)
(As mentioned above, the prototype of new object () is null. In fact, this is what I take for granted, because the prototype chain must end with null. however, in fact, the prototype _ PROTO _ of any object created with new is not null (fortunately, this variable can be accessed), but an internal object called an internal object, because it is a system-built object
Object, globally unique. the only difference between object creation and object creation is that its _ PROTO _ is null. we can use _ PROTO _ to access this variable, but we cannot delete it. JavaScript does not have the ability to force Delete objects, but we can change it, such as adding properties to it, the result is that all JavaScript objects have this attribute under the prototype chain, which is why some browsers do not provide the _ PROTO _ attribute access)