20150206--js consolidation and strengthening 4-02

Source: Internet
Author: User

V. Attribute traversal and deletion 1, use for...in ... Traverse Custom Object Properties

Basic syntax:

For...in ... (mainly to complete the traversal of the object)

Example code:

Operating effect:

Description: In 17 lines of code, you cannot use p1.i as a way to customize the traversal of object properties, otherwise the undefined will pop up because when the code recognizes that the P1 object considers p1.i to be the I property of the P1 object, the system pops up 3 undefined because the I attribute is not defined. 2. Use for...in ... Structure implements traversal of system objects

Window

Document

Example code:

Operating effect:

3. Property Delete operation

In JavaScript, sometimes we may not need a property of an object, we can use the DELETE keyword to implement the deletion of the custom property

Basic syntax:

Delete Object. Properties

Delete the specified property

Example code:

Operating effect:

Vi. member Methods

1. Basic syntax:

Object. property = First address of function

Example 1: Adding a speak speaking method to an object

Example 2: Creating an object P2, adding a speak speaking method to P2

Observing the code above is exactly the same as the member method of the P1 object in Example 1, which seems to overlap the code and improve the problem

Example 3: Improving the title

Example 4: Because the above code has a lot of repetitive code, then we can encapsulate the above code

Vii. JSON object 1, what is JSON

Object refers to an unordered collection of attributes

The so-called "set" is a set of named/value pairs, separated by a comma between the name/value pairs.

In JS, you can use {} to represent this set 2, the basic syntax

var variable ={};

The first type: {' attribute ': ' property value ', ' property ': ' Property value ', ' property ': ' Property value '}

The second type: {property: Value of Property, property: Value of Property, property: Value of Property}

Third: {attribute: ' property value ', Property: ' Property value ', Property: ' Property value ',} 3, QuickStart

Example 1: Describing a person's information through a JSON object

4. JSON object from "where"

Through the above code can prove:

The JSON object is an instance of the object class, and in JavaScript, the object class is the base class for all classes, all of which inherit the properties of the object class.

JSON objects are primarily used for saving large amounts of data

Example 2: Saving more than one person's information through a JSON object

5. PHP and JSON

PHP provides two functions for generating and parsing JSON formats

Json_encode (array or object)

Arrays typically take an associative array of

Json_decode (JSON format string)

Ajax XML

Ajaj JSON

1) Convert the array into JSON-formatted data

2) Convert objects into JSON-formatted data

Note: In the Json_encode function, currently only support UTF-8, if it is GBK or GB2312 through the iconv to convert, otherwise can not use or can not get normal data

3) Convert two-dimensional arrays to JSON-formatted data

4) Json_decode Code parsing

Json_decode ($json object, [bool])

Function: Reverse conversion of JSON format data

Parameter description:

The first parameter is JSON-formatted data

The second argument is a Boolean-type value, and if true, returns the array data of type array and, if False or not, returns an instance (object) of the Stdclass class directly

Do not add true parameter effect

Case: Complete the API call to history today via Php+json

http://www.juhe.cn/aggregating data

A) Copy the URL address http://japi.juhe.cn/toh/toh?key= the key&v=1.0&month=11&day=1 you requested

Parameter description

Key: Keys

V: Version 1.0

Month: Months

Day: Days

b) Through the interface, it belongs to the GET request

The effect is as follows:

6. Adding a member method to a JSON object

Operating effect:

Viii. prototype chain 1, what is a prototype object?

Answer: When the system loads the constructor, the system automatically generates an object, which we call the prototype object.

Constructors and prototype objects are independent of each other in memory, but they are also associated with:

There is a prototype property in the constructor that points to the prototype object

Prototype object also exists property pointing to the constructor

2, the role of prototype objects?

When an instance of the constructor accesses a nonexistent property, the system is automatically searched for the constructor's prototype object, which is used directly if found.

When we cannot find the attributes or methods we want in some existing frameworks or objects, we can use the prototype object to append the required properties or methods to the prototype object. 3. How to add a property or method to a prototype object

Constructor. prototype. property = value of the property

4. Discussion: Where did the prototype object come from?

Description: When the system creates a prototype object, the system automatically executes the following code

constructor. prototype = new Object ();

Because prototype objects are instances of the object class, our prototype objects automatically inherit the properties and methods in the object class, based on the object-oriented principle.

Example code:

Note: When we run to line 14th, the system automatically accesses the Hasownpropery property of the P1 object, but because the hasOwnProperty attribute is not present in the system, it goes to the prototype object of the person constructor and is not found, Again, because all the prototype objects are instances of the object class, the prototype object automatically inherits all the properties and methods under the object class, so it is found that the line code executes normally, and the schematic is as follows:

We call this a prototype inheritance, and by solution we can also prove that the object class is the base class for all classes.

Note: In JavaScript, when an object refers to a nonexistent property or method, the system first looks in the prototype object of the current constructor, and if it is not found, it goes to the previous layer of the prototype object to look for, until the object prototype objects, we call this link lookup relationship is called the prototype chain.

Job: Extending the functionality of an array class: (array)

Add a method for each array object to find where an element is located

var arr = [10,20,30,40,50];

Arr.find (40);

2, expand the function of the digital class (number)

Adds a method to a numeric object that has an arbitrary number of integers, and then adds all the arguments together and returns the sum

Vari = 10;

I.sum (10,30,40,50,60,60);

20150206--js consolidate and strengthen 4-02

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.