The object class of the JavaScript reference type

Source: Internet
Author: User
Tags hasownproperty


The object class in ECMAScript is similar to the object class in Java, and all classes in the ECMAScript are inherited by this class, andall properties and methods in the object class appear in other classes . So understanding the object class makes it easier to understand other classes.


Creation of Objects


JavaScript objects are containers for properties, where each property has a name and value. The name of the property can be any string, including an empty string. The property value can be any value other than the undefined value.

There are no types of objects in JavaScript. It has no restrictions on the value of the new property's name and property. Objects and apply to collection and management data. Objects can contain other objects, so they can be easily represented as a tree or graphic structure.

The object type is used to create custom objects (instances) in two ways:


Add object construction method after new

var cat = new Object (); cat.name = "Tomcat"; cat.age = 3;alert ("Cat's name is" + Cat.name + "and it is" + cat.age);

Access to object properties can take the object name directly. the way the property name is.


object literal notation

<pre name= "Code" class= "javascript" >var dog = {"Name": "Hotdog", "Age": 3}

The property names and attribute values in the inside are in the form of Key:value key-value pairs, where the quotation marks above the key are usually omitted.

var dog = {name: "Hotdog", Age:3}alert ("Dog's name is" + dog["name"] + "and it is" + dog["age"]);

Object properties can also take the form of an object name [property name] .


access to object properties


There are two ways to access object properties: Dot notation and square brackets notation.

    • Point notation object name. Property name
    • Square brackets notation Object name [property name]


Advantages of square brackets notation
    • Properties can be accessed through variables

var  pname= "name"; alert (Cat[pname]);

    • You can also use square brackets if the property name contains a character that causes a syntax error, or if the property name uses a keyword or reserved word.

The property name contains a space so you cannot access it by using dot notation.

var catobj = {name: "Tomcat", "lovely brother": "Tom"}var pName = "name"; alert (Catobj[pname]); Output Tomcatalert (catobj["lovely brother"]); Output Tom

Point notation Advantages


If the hierarchy of attributes is deep, it is troublesome to use square brackets notation, but using dot notation makes it easy to access the required properties in a layer of layers.


var animals = {dog: {name: "Hotdog", Age:4},cat: {name: "Tomcat", Age:3}}

Point notation Access Dog's Name property

alert (animals.dog.name);

Square Bracket notation access to the dog's Name property

Alert (animals["Dog" ["name"]);

Using square brackets notation, if the property is more, you must use [and] all the time, and also enclose the attribute name in quotation marks, rather than the point notation directly.

Compare two ways: Access attributes are recommended to use dot notation, unless you choose to use square brackets when you can't use dot notation


properties of the object class


The object class has the following properties:

    • Constructor---A reference to the function that created the object. For the object class, the pointer points to the original object () function.
    • Prototype---A reference to the object's prototype of the object. For all classes, it returns an instance of Object objects by default.

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

methods of the object class


The object class has the following methods:

    • hasOwnProperty---to determine whether an object has a specific property. The property must be specified with a string (for example: Obj.hasownproperty ("name")).
    • isPrototypeOf (object)---Determine whether the object is a prototype of another object.
    • propertyIsEnumerable---Determine whether a given property can be enumerated with a for...in statement.
    • The toString ()---Returns the original string representation of the object.
    • The 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),propertyisenumerable (property)these three methods are described in detail after the prototype prototype, which is not introduced here.


The object class of the JavaScript reference type

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.