JavaScript notes one JS and JSON basics use instructions _javascript tips

Source: Internet
Author: User
Tags function definition
The code in JavaScript is only one form, or function.

Note: The above words are lowercase and do not confuse JavaScript built-in functions such as number, String, Object, function, and JavaScript language is case-sensitive.

typeof (NULL) returns object, but null is not object.

The code for JavaScript has only one form of function, which is the type of functions. The functions are written in "defined" and "variable".

A defined function statement is executed preferentially. After the function definition executes, the other statement code is executed sequentially, and JavaScript executes in a paragraph.

Let's take a look at the following code:
Copy Code code as follows:

var myfunc = function ()
{
Alert ("Hello");
};
MyFunc (); Call MyFunc for the first time, output hello
MyFunc = function ()
{
Alert ("Yeah");
};
MyFunc (); The second call to MyFunc will output yeah


The results of this program tell us that after the first call to the function, the function variable is given a new function code body, so that when the second call to the function, there is a different output.

Okay, so we're going to change the above code to the form of a defined function:

Copy Code code as follows:

function MyFunc ()
{
Alert ("Hello");
};
MyFunc (); Here call MyFunc, output yeah instead of Hello
function MyFunc ()
{
Alert ("Yeah");
};
MyFunc (); Here call MyFunc, of course output yeah


Logically, two functions with identical signatures should be illegal in other programming languages. But in JavaScript, that's true. Instead of parsing and executing the program in one line, the JavaScript execution engine is a section of analysis performed. The code logic defined by the first function statement is overwritten by the second function definition statement before the first call to MyFunc. So, both calls are performed on the last function logic.

Creating objects

Copy Code code as follows:

<script type= "Text/javascript" >
function Test () {
var bo = {}; Create an Object
Bo. Name = "John"; An attribute of an object
Bo. Age = 18;
Bo.showinfo = function () {alert (bo. Name + "" + bo. Age); A method of an object
Alert (bo["name"]); You can access an object as an array by using the property name as the subscript
bo["Showinfo"] (); You can call a method by using the object as the subscript with the method name as the array
Iterate through all the properties and methods in the object and output its type
For (Var s in Bo) {
Alert (S + "is" + typeof (Bo[s]));
}
}
</script>


JSON provides a very simple way to create objects, JavaScript Object Notation (abbreviated JSON), which translates to Chinese as "JavaScript objects notation."

To create an object without any attributes:
var o = {};

Create an object and set properties and initial values:
var person = {Name: "Angel", Age:18, married:false};

Create an object and set properties and methods:
var speaker = {text: "Hello World", Say:function () {alert (This.text)}};

Create a more complex object, nest other objects and arrays of objects, and so on:
Copy Code code 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 form of JSON is a list of items that are included with a large "{}" number, separated by commas "," and the item is a property name and attribute value separated by a colon ":". This is a typical dictionary representation and once again shows that the object in JavaScript is the dictionary structure. No matter how complex the object is, it can be created and assigned by a JSON code.

In fact, JSON is the best serialized form of a JavaScript object, which is simpler and more space-saving than XML. objects can be used as a JSON-form string to freely communicate and exchange information between networks. And when you need to turn this JSON string into a JavaScript object, just use the Eval function, a powerful digital conversion engine, and immediately get a JavaScript memory object. It is the simplicity of JSON that makes her a star on the Ajax stage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.