Javascript Object learning notes, object learning notes

Source: Internet
Author: User

Javascript Object learning notes, object learning notes

Constructor
New Object ()

New Object (value)

Parameters
Value
Optional parameter. It declares the original value (that is, Number, Boolean, or String) of the Number object, Boolean object, or String object ). This object is not supported in versions earlier than JavaScript 1.1 and ECMAScript Vl.

Return Value

If the value parameter is not passed to the constructor, it returns a newly created Object instance. If the original value parameter is specified, the constructor will create and return the packaging object of the original value, that is, the Number object, Boolean object, or String object. When the Object () constructor is called as a function without the new operator, its behavior is the same as that when the new operator is used.

Attribute
Constructor
References A JavaScript function, which is an object constructor.

Method

1. hasOwnProperty ()
Check whether the object has a locally defined (non-inherited) attribute with a specific name.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var o = new Object ();
O. name = "Tom ";
Alert (o. hasOwnProperty ("name"); // true
Alert (o. hasOwnProperty ("age"); // false
</Script>

2. isPrototypeOf ()

Syntax
Object. isPrototypeOf (o)

Parameters
O
Any object.


Return Value
If the object is an O prototype, true is returned. If o is not an object, or the object is not the original o type, false is returned.

Description
The JavaScript object inherits the attributes of the prototype object. An object prototype is referenced by the prototype attribute of the constructor used to create and initialize the object. The isPrototypeOf () method provides a method to determine whether an object is a prototype of another object. This method can be used to determine the class of the object.

Example

Copy codeThe Code is as follows:
Var o = new Object (); // create an Object
Object. prototype. isPrototypeOf (o) // true: o is an Object
Function. prototype. isPrototypeOf (o. toString); // true: toString is a Function.
Array. prototype. isPrototypeOf ([1, 2, 3]); // true: [1, 2] is an Array
// The following is another method for executing the same test
(O. constructor = Object); // true: o was created with Object () constructor
(O. toString. constructor = Function); // true: o. toString is a function
/Prototype indicates that the object itself is a prototype object. The following call returns true.
// Describes the attributes of the Function following Function. prototype and Object. prototyp.
Object. prototype. isPrototypeOf (Function. prototype );

3. ProertyIsEnumerable ()

Syntax
Object. propertyIsEnumerable (propname)

Parameters
Propname
A string containing the name of the object prototype.

Return Value
If the object has a non-inherited property named propname and the property is enumerable (that is, the for/in loop can be used to enumerate it), true is returned.

Description
You can use the for/in statement to traverse the "enumeration" attribute of an object. However, not all attributes of an object can be enumerated. The attributes added to an object through JavaScript code can be enumerated, and the predefined attributes (such as methods) of an internal object) it is generally not enumerative. The propertylsEnumerable () method provides a method to distinguish between enumerated and non-enumerated attributes. Note that, according to the ECMAScript standard, the propertyIsEnumerable () method does not detect the prototype chain, which means that it only applies to the partial attributes of the object and cannot detect the enumerable nature of the inherited attributes.

Example

Copy codeThe Code is as follows:
Var o = new Object (); // create an Object
O. x = 3.14; // define-attributes
O. propertyIsEnumerable ("x"); // The true attribute x is local and enumerative.
O. propertyIsEnumerable ("y"); // false: o has no attribute y
O. propertyIsEnumerable ("toString"); // false: The toStrlng attribute is inherited.
Object. prototype. propertyIsEnumerable ("toString"); // false: Enumerated

Bug

When the standard method restricts propertylsEnumerable () to only detect non-inherited attributes, it is obviously wrong. Internet Explorer 5.5 implements this method according to the standard. The propertyIsEnumerable () method implemented by Nestacpe 6.0 considers the prototype chain. Although this method is desirable, it conflicts with the standard, So Netscape 6.1 modifies it to match IE 5.5. This method is not so useful because of this error in the standard.

Copy codeThe Code is as follows:
<Script>
Var obj = new Object ();
Obj. title = 'aaa ';
Obj. funb = function (a, B)
{
Alert (a + B );
}
Alert (obj. title );
Obj. funb (1, 2 );
</Script>

Here is a method
******************************

Copy codeThe Code is as follows:
<Script language = "javascript">
Function object (value, a, B ){
This. title = value;
This. funb = function (){
This. a =;
This. B = B;
Alert (a + B );
}
}
Var obj = new object ("aaa", 1, 2 );
Alert (obj. title );
Obj. funb ();
// Add a new method for the object
Object. prototype. name = "123456 ";
Alert (obj. name );
</Script>

This is another method

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.