Object Class of JavaScript reference type

Source: Internet
Author: User

Object Class of JavaScript reference type


The Object class in ECMAScript is similar to the Object class in Java.InheritanceCome,All attributes and methods in the Object class will appear in other classes.So you can better understand other classes by understanding the Object class.


Object Creation


JavaScript objects are property containers. Each property has a name and a value. The attribute name can be any string including an empty string. The attribute value can be any value except the undefined value.

Objects in JavaScript are non-typed. It has no restrictions on the names and values of new attributes. Object and applicable to data collection and management. Objects can contain other objects, so they can be easily expressed as tree or graphic structures.

The Object type is used to create a custom Object (Instance). There are two ways to create a custom Object:


Object constructor added after new

var cat = new Object();cat.name = "tomcat";cat.age = 3;alert("cat's name is " + cat.name + " and its age is " + cat.age);

You can directly useObject Name. attribute name.


Object literal representation

var dog = {"name":"hotdog","age":3}

The attribute names and attribute values are in the form of key: value pairs. The quotation marks on the key can be omitted.

var dog = {name:"hotdog",age:3}alert("dog's name is " + dog["name"] + " and its age is " + dog["age"]);

The object attribute method can also be used.Object Name [attribute name].


Access to Object Attributes


There are two access methods for object attributes: dot notation and square brackets notation.

Point representationObject Name. attribute nameSquare bracketsObject Name [attribute name]


Advantages of square brackets
You can use variables to access attributes.

var  pName="name";alert(cat[pName]);

If the attribute name contains characters that may cause syntax errors, or the attribute name uses keywords or reserved words, it can also be represented in square brackets.
cat["lovely brother"]="Tom"; 

Attribute names contain spaces, so they cannot be accessed using dot notation.

var catObj = {name : "tomcat","lovely brother" : "tom"}var pName = "name";alert(catObj[pName]); // output tomcatalert(catObj["lovely brother"]); // output tom

Point representation advantages


If the attribute layers are deep, it is difficult to use square brackets. However, you can use dot notation to easily access the required attributes.


var animals = {dog : {name : "hotdog",age : 4},cat : {name : "tomcat",age : 3}}

Access the name attribute of dog in dot notation

alert(animals.dog.name);

Use square brackets to access the name attribute of dog

alert(animals["dog"]["name"]);

Square brackets are used for representation. If there are many attributes, [and] must be used one by one, and attribute names must be enclosed in quotation marks. This is not as straightforward as dot notation.

We recommend that you use dot notation unless you cannot use dot notation.


Attributes of the Object class


The Object class has the following attributes:

Constructor --- a reference to the function used to create an object. For Object classes, this pointer points to the original object () function. Prototype --- reference the object prototype of the object. For all classes, it returns an instance of the Object by default.

var obj = new Object();alert(obj.constructor);// output /*function Object() {    [native code]}*/

Object Class Method


The Object class has the following methods:

HasOwnProperty --- determines whether an object has a specific property. This attribute must be specified with a string (for example, obj. hasOwnProperty ("name ")). IsPrototypeOf (object) --- determines whether the object is a prototype of another object. PropertyIsEnumerable (property) --- determines whether a given property can be used... In statement. ToString () --- returns the original string representation of the object. ValueOf () --- returns the original value that best fits the object.

var tomObj = {name : "tomcat",age : 4}alert(tomObj.hasOwnProperty("name")); // output truealert(tomObj.hasOwnProperty("sex")); // output falsealert(tomObj.toString()); // [object Object]alert(tomObj.valueOf());  // [object Object]


HasOwnProperty (), isPrototypeOf (object), and propertyIsEnumerable (property) methods are described in detail after prototype is prototype.


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.