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

Detailed introduction to js objects

零下一度
Release: 2017-06-28 09:36:06
Original
1374 people have browsed it

a. JS objects are all associative arrays

b. inherit(); returns a new object that inherits the properties of the prototype object p

Object method :

Create (create) Set (set) Search (query) Delete (delete) Test (test) and enumerate (enumerate)

Methods to create objects:

Object literal keyword new Object.create()

in es5 var aa=Object.create({"x":0,"y":1})

Reading of attributes Retrieval and modification:

1. Through . connection Attributes cannot be changed and cannot be changed at runtime

2. Through object['xxx']; Attributes can be variables, such as object[' xx'+a]; a can be a variable, so the attribute is undefined and can be changed during operation

 3. Querying for an attribute that does not exist will return undefined

 4. Querying an object property, if the object does not exist, an error will be thrown. If you query the attributes of an object and prevent it from reporting an error, you can do this:

var a=b&&b.c&&b.c.d;

Deletion of attributes

1. Delete can only interrupt Open the connection between the host and the host object without operating the properties in the properties. Global object properties created through variable declarations or function declarations cannot be deleted. True is returned on success and false is returned on failure

  delete a.b// a no longer has the attribute b

  delete a['b']//a no longer has the attribute b

Detection of the attribute

 1.in operator, hasOwnProperty(), propertyIsEnumerable()

  The property name on the left side of in, and the other side is the object. If the object's own property or inherited property contains this property, it returns true, otherwise false

  var a={x:1 } a.hasOwnPreperty('x');//true

PropertyIsEnumerable() is an enhanced version of hasOwnPreperty(). Only the property that is enumerable and belongs to this object will return true

 2. The simplest method! ==Is it undefined

Enumeration of properties

1. All properties added to the object in the code are enumerable. In for/in we need to skip some properties

  for(p in o){

  if(!o.hasOwnproperty(p)) continue ;//Skip inherited properties

  }

for(p in o){

  if(typeof o[p]==="function") continue ;//Skip method

  }

 2. In There are two more functions in es5

 Object.keys();//Returns an array, which consists of the enumerable own properties of the object

 Object.getOwnPropertyNames(); //Return the names of all the own properties in the object

Attribute getter and setter (accessor attribute)

 1.var 0={

  a:1,/ /Ordinary data attributes

   //Accessor attributes are functions defined in pairs

  get b(){This is the function body},

  set c(){This is the function body}

   }

Three attributes of the object

 1. Prototype attributes:

 var p ={x:1};//Define a prototype object

  var o=Object.create(p);Use this prototype to create an object

  p.isPrototypeOf(0);// true,o ​​inherits from p

Object.prototype.isPrototypeOf(o);//p inherits from Object.prototype

2. Class attributes

3. Extensibility Determine whether the object is extensible by passing the object into Object.esExtensible() Cannot be converted back

 Object.seal() can not only set the object as non-expandable, but also set all its own properties as non-configurable

 isSealed() detects the object Whether it is closed

Object.freeze() Frozen, not only unconfigurable, but also readable

Object.isFrozen() Check whether the object is frozen

Object serialization

 1:JSON.stringify();//Convert to JSON string

 2::JSON.parse();//Convert to object

Object method:

 1.toString();

 2.toLocaleString();

 3.toJSON();

 4:valueOf();

The above is the detailed content of Detailed introduction to js objects. For more information, please follow other related articles on the PHP Chinese website!

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