The data in JavaScript is concise. Simple data only includes undefined, null, boolean, number, and string, and complex data contains only one type, namely, object. The code in JavaScript is only reflected in one form, that is, function.
Note: The above words are in lower case. Do not confuse them with JavaScript built-in functions such as Number, String, Object, and Function. The JavaScript language is case sensitive.
Typeof (null) returns an object, but null is not an object.
JavaScript code has only one function form, and function is the function type. Functions are written in the form of "Definition" and "variable ".
The defined function statements are executed first. After the function definition is executed, other statement code is executed in sequence. JavaScript is executed in segments.
Let's take a look at the following code:
The Code is as follows:
Var myfunc = function ()
{
Alert ("hello ");
};
Myfunc (); // call myfunc for the first time and output hello
Myfunc = function ()
{
Alert ("yeah ");
};
Myfunc (); // The second call to myfunc will output yeah
The result of the program running tells us that after the first function call, the function variable is assigned a new function code body, so that different outputs appear when the function is called for the second time.
Now, let's change the above Code to a defined function:
The Code is as follows:
Function myfunc ()
{
Alert ("hello ");
};
Myfunc (); // call myfunc here and output yeah instead of hello
Function myfunc ()
{
Alert ("yeah ");
};
Myfunc (); // call myfunc here, of course, output yeah
The two functions with identical signatures should be invalid in other programming languages. But in JavaScript, that's right. The JavaScript execution engine does not analyze and execute programs one by one, but analyzes and executes programs one by one. Before calling myfunc for the first time, the Code logic defined by the first function statement was overwritten by the second Function Definition Statement. Therefore, the last function logic is executed for both calls.
Create object
The Code is as follows: