Object of JavaScript

Source: Internet
Author: User
Tags hasownproperty

Objects are data that have properties and methods.

In JavaScript, everything is the object. Array, function, string ...

Definition and access to objects

In JavaScript, we can use dots (.) and square brackets ([]) to access the object, and there are several ways to define the object:

1, object literal, such as: var obj = {}

2, new keyword, such as: var obj = new Object ()

3, Object.create (), such as: var obj = object.create (object.prototype)

4, constructor, Factory mode, prototype mode, constructor + prototype, etc., such as function F () {} var obj = new F ()

5. Delete the properties of the object with the Delete keyword, you cannot delete the properties on the prototype

1  //Create an empty object2  varobj = {};3  varObj1 =NewObject ();4  //adding properties and methods to an object5Obj.name = "Lily";6Obj.sayhi =function(){7Cons.log ("Hello World");8 }9 Ten  //to create an object with properties and methods One varObj2 = { AName: "Kimmy", -Age:12, -Sayhi:function(){ theConsole.log ("Hello Kimmy"); -     } - } -  +  //creating an object from a constructor function - functionCar (name,color,price) { +      This. Name =name; A      This. color =color; at      This. Price =Price ; - } - varCar =NewCar ("BMW", "Silver", "55"); -  - //accessing the properties and methods of an object -Car.name;//back to BMW incar["Color"];//Back to Silver -  to //Factory mode Method + function Createperson(name,age,job) { -        varperson =NewObject (); thePerson.name =name; *Person.age =Age ; $Person.job =job;Panax NotoginsengPerson.sayname =function(){ - alert (person.name); the        } +        returnPerson ; A } the      + varPerson1 = Createperson ("zh", "$", "Doctor"); -Person1.sayname ();

properties and methods for object instances

1. Constructor Property-stores the constructor of the current object

2. Prototype Property-The prototype property is set when the object is created and is used to inherit the property.

3, hasOwnProperty (property name) method-detects whether the given property name is a property of the object itself, and returns false for inherited properties

4. isPrototypeOf (object) method-Determines whether an object is on the prototype chain of an object

5. Propertyisenumberable (attribute name) method-is an enhanced version of hasOwnProperty that returns True when the object has its own and enumerable properties

6, ToString (), tolocalstring (), ValueOf () method

7. For...in ... Iterating over an object's enumerable properties

1 //Constructor2 varobj = {3Name: "Lee",4Age:12,5Sayfunction(){6Console.log ("Hello");7     }8 }9Obj.constructor;//returns the function Object () {}Ten  One //hasOwnProperty AObj.hasownproperty ("Age");//returns True -Obj.hasownproperty ("toString");//returns False,tostring is an inherited property -  the //propertyisenumerable -Obj.propertyisenumerable ("say");//returns True -Obj.propertyisenumerable ("toString");//returns false,tostring non-enumerable -  + //for...in Traversal -  for(varOinchobj) { +Console.log (o);//returns the name, age, say A } at  - //isprototypeof - functionMyObject () {} - varobj =NewMyObject (); -Console.log (Object.prototype.isPrototypeOf (obj));//returns True because MyObject is inherited from object objects, and inheritance is implemented through prototype, so the prototype of object must be on the prototype chain of the MyObject object instance

Attributes of a property

In JavaScript, attributes are classified as data attributes and accessor attributes, and the data is true only as a value, and accessor attributes are defined by getter and setter.

Data attributes are writable (writable), configurable (configurable), Enumerable (enumberable), and value attributes. We can set these four features through Object.defineproperty () and Object.defineproperties ().

Writable-Indicates whether the property value can be modified by default to True

Configurable-Indicates whether the property can be removed by using delete, which defaults to True

Enumberable-Indicates whether the property can be enumerated by for...in, which is true by default

Value-Represents the values of the property, which defaults to undefined

1 //the name, age, and sex properties in the Obj object are now removable2 varobj = {3Name: "Jimmy",4Age:12,5Sex: "M"6 }7Obj.name;//back to Jimmy8 DeleteObj.name;//returns True9Obj.name;//Back to undefinedTen  One //we can use Object.defineproperty () to set up writable, configurable, enumerable, and value, only one attribute can be manipulated, to operate on multiple properties, by Object.defineproperties () to set  A /* - * Object.defineproperty (obj,propertyname,option) - * Obj-the object that needs to be set the * The name of the property to be set in the Propertyname-obj object - * Option-an object that configures its 4 properties - */     - varObj1 = { +Name: "Jimmy", -Age:25, +Sex: "M" A } atObject.defineproperty (obj1, "name",{ -Writable:false,//false is not writable; True can be written -Configurable:false,//false is not configurable; true configurable -Enumberable:false,//false cannot be enumerated; True enumerable -Value: "Kimmy"//set a value for name - }); inObj.name;//back to Kimmy - DeleteObj.name;//returns false toObj.name;//back to Kimmy +  - /* the * Object.defineproperties (obj,option) * * Obj-the object that needs to be set $ * option-a mapping table that configures its 4 attributes separately for the properties that need to be set in objPanax Notoginseng */    - varObj2 = { theName: "Lily" +Age:25 A } the object.defineproperties (obj2,{ + name:{ -Writable:true,//false is not writable; True can be written $Configurable:false,//false is not configurable; true configurable $Enumberable:false    //false cannot be enumerated; True enumerable -     }, - age:{ theWritable:true,//false is not writable; True can be written -Configurable:false,//false is not configurable; true configurableWuyiEnumberable:true    //false cannot be enumerated; True enumerable the     } -});

Accessor properties do not have the writable (writable) and value values, but they use a bunch of getter and setter functions.

Get-Called when a property is read, default is undefined

Set-Called when the property is set, the default is undefined

1 //Defining Accessors2 varobj = {3Age:15,//Data Properties4 5     //Accessor Properties6 get Type () {7         return  This. Age > 18? "Adult": "Underage";8     },9 set Type (age) {Ten          This. Age =Age ; One     } A }; -Obj.type;//Return to Minors -Obj.type = 20; theObj.type;//Return to adulthood -Obj.age;//return

Note:The GET, set, and function bodies are not separated by a colon, but with a space

This article is only for oneself in peacetime work study to make notes to use! If there are errors please point out!!!

Object of JavaScript

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.