Learning and summarizing of JavaScript objects

Source: Internet
Author: User

A. an object is a basic data type of JavaScript, a composite value, a container for attributes, and a property that consists of key-value pairs.

two. Js object is divided into: Built-in object, host object, custom object

three. creation of objects

(1) object Direct Volume: a mapping table consisting of several name/value pairs, which is an expression that creates and initializes a new object for each operation of the expression.

For example:

var empty_oject={}; define an object without any attributes

var point={x:0,y:0}; An object that defines two properties

var stooge={

Name: " Xiyin " ,

Sex: " Girl " ,

"first-name":"XI"

}

In the object's polygon amount, if the property name is a valid JavaScript identifier and is not a reserved word, you can enclose the attribute name in quotation marks, and the above first-name is not a valid name, so.

(2) Create an object from new: Creates and initializes a new object, followed by a function (called a constructor) called by the keyword new.

For example:

Var 0=new object ();// Create an empty objects, with var 0={}; equivalent

var a =new array ();// Create an empty array with var a=[]; equivalent

var d=new date ();//Create a date Object that represents the current event

(3) object.create (), defined by ES5 , which is used to create a new object, the first parameter is the prototype of the object, and the second is an optional parameter to further describe the properties of the object.

Var o3=object.create (Object.prototype); with the Var 0=new Object ();// creates an empty object, with the var 0={}; equivalent

Four. Object Properties Query (retrieve) and settings (update), delete properties

(1) can be passed . number or square brackets "" To get the value of the property

Object.property

object["Property"]

(2) The value of the object can be updated by an assignment statement, if the property name already exists in the object, then the value of this property will be replaced , If the object does not have that property name, then the property is extended to the object.

stooge["first-name"]= " Yin " ;

(3) The delete operator can delete the object's properties Delete stooge["first-name the],delete operator can only delete its own property and cannot delete an inherited property

Five. Detection Properties: Determine if a property exists in an object

(1) in operator: The left side is the property name (string), the right side is the object, if the object's own property, or the Inheritance property contains this property, returns true.

var o={x:1}

"x" in o//true

"y"ino//false

"tostring" in o//true o inherits the ToString Property

(2) The hasOwnProperty () method of the object is used to detect if the given name is an object's own property, and it returns false for the inherited property .

(3)propertyisenumerable (), only if the attribute is detected and the enumerable of this property is true , It only returns true

Six. Serializing Objects

A serialized object refers to the state of an object being converted to a string, or a string to an object.

ES5 provides built-in functions json.stringify () and json.parse () to serialize and revert to JavaScript Objects (Json--javascript object notation JavaScript object notation)

Seven . Object Methods

(1)toString () method, without parameters, returns a string representing the value of the object that called the method


This article is from the "dream need to adhere to" blog, please be sure to keep this source http://xiyin001.blog.51cto.com/9831864/1761142

Learning and summarizing of JavaScript objects

Related Article

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.