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:
Copy codeThe 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:
Copy codeThe 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
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function test (){
Var bo ={}; // create an object
Bo. Name = "Zhang San"; // an attribute of the object
Bo. Age = 18;
Bo. showInfo = function () {alert (bo. Name + "" + bo. Age) ;}; // a method for Object
Alert (bo ["name"]); // you can use the attribute name as the subscript of an array to access the attribute.
Bo ["showInfo"] (); // you can call a method when the array uses the method name as the subscript.
// Traverse all the attributes and methods in the object and output their types
For (var s in bo ){
Alert (s + "yes" + typeof (bo [s]);
}
}
</Script>
JSON provides a very simple method for creating objects. JavaScript Object Notation (JSON) is translated into Chinese as "JavaScript Object Notation ".
Create an object without any attributes:
Var o = {};
Create an object and set attributes and initial values:
Var person = {name: "Angel", age: 18, married: false };
Create an object and set attributes and methods:
Var speaker = {text: "Hello World", say: function () {alert (this. text )}};
Create a more complex object and nest other objects and object arrays:
Copy codeThe Code is as follows:
Var company =
{
Name: "Microsoft ",
Product: "softwares ",
Chairman: {name: "Bill Gates", age: 53, Married: true },
Employees: [{name: "Angel", age: 26, Married: false}, {name: "Hanson", age: 32, Marred: true}],
Readme: function () {document. write (this. name + "product" + this. product );}
};
The JSON format is the list of projects included with the large "{}". Each project is separated by a comma (,), and the project is a colon (:):. This is a typical dictionary representation, and again shows that the objects in JavaScript are dictionary structures. No matter how complex an object is, it can be created and assigned a value by a JSON code.
In fact, JSON is the best serialization form for JavaScript objects, which is simpler and more space-saving than XML. An object can be a JSON string and transmitted and exchanged freely between networks. To convert the JSON string into a JavaScript Object, you only need to use the eval function, a powerful digital conversion engine, to immediately obtain a JavaScript Memory Object. Because of JSON's simplicity and simplicity, she became a dazzling star on the AJAX stage.