Home > Web Front-end > JS Tutorial > body text

Javascript notes 1 js and json basic usage instructions_javascript skills

WBOY
Release: 2016-05-16 18:26:41
Original
865 people have browsed it

Code in JavaScript is only reflected in one form, which is function.

Note: The above words are all lowercase. Do not confuse them with JavaScript built-in functions such as Number, String, Object, Function, etc. JavaScript language is case-sensitive.

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

JavaScript code only has one form: function, and function is the type of function. Functions can be written in "definition" and "variable" ways.

Defining function statements will be executed first. After the function definition is executed, other statement codes will be executed in sequence. JavaScript is executed piece by piece.

Let’s take a look at the following code:

Copy the code The code is as follows:

var myfunc = function ()
{
alert("hello");
};
myfunc(); //The first time myfunc is called, output hello
myfunc = function ()
{
alert("yeah");
};
myfunc(); //The second call to myfunc will output yeah


The results of running this program tell us: after the first call to the function, the function variable is assigned a new function code body, causing different output when the function is called the second time.

Okay, let’s change the above code into a defined function form:

Copy code The code is as follows:

function myfunc ()
{
alert("hello");
};
myfunc(); //myfunc is called here , output yeah instead of hello
function myfunc ()
{
alert("yeah");
};
myfunc(); //myfunc is called here, of course output yeah


It stands to reason that two functions with exactly the same signature should be illegal in other programming languages. But in JavaScript, this is true. The JavaScript execution engine does not analyze and execute the program line by line, but analyzes and executes it piece by piece. Before calling myfunc for the first time, the code logic defined by the first function statement has been overwritten by the second function definition statement. Therefore, both calls execute the last function logic.

Create object

Copy code The code is as follows:

< ;script type="text/javascript">
function test() {
var bo = {}; //Create an object
bo.name = "Zhang San"; //One of the objects Attribute
bo.age = 18;
bo.showInfo = function() { alert(bo.Name" " bo.Age); }; //A method of the object
alert(bo["Name "]); //You can use the object as an array and use the property name as a subscript to access properties
bo["showInfo"](); //You can use the object as an array and use the method name as a subscript to call methods
//Traverse all properties and methods in the object and output their types
for (var s in bo) {
alert(s " is " typeof (bo[s]));
}
}



JSON provides a very simple method for creating objects, JavaScript Object Notation (abbreviation JSON), translated into Chinese is "JavaScript Object Notation" Law".

Create an object without any properties:
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 object arrays, etc.:
Copy code The 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 format of JSON is a list of items enclosed in braces "{}", each item is separated by a comma ",", and the item is an attribute name and attribute value separated by a colon ":". This is a typical dictionary representation, and it once again shows that objects in JavaScript are dictionary structures. No matter how complex the object is, it can be created and assigned with a JSON code.

In fact, JSON is the best serialization form of JavaScript objects. It is more concise and space-saving than XML. The object can be used as a string in the form of JSON to freely transmit and exchange information between networks. When you need to turn this JSON string into a JavaScript object, you only need to use the eval function, a powerful digital conversion engine, to get a JavaScript memory object immediately. It is precisely because of the simple and simple natural beauty of JSON that she becomes a dazzling star on the AJAX stage.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template