Javascript Object Object Learning Notes _ Basics

Source: Internet
Author: User
Tags object object hasownproperty

Constructors
New Object ()

New Object (value)

Parameters
value
An optional parameter that declares the original value (that is, a number, Boolean, or String) to convert to a numbers object, a Boolean object, or a string object. This object is not supported by versions prior to JavaScript 1.1 and ECMAScript VL.

return value

If the constructor is not passed the value parameter, it returns a newly created instance of object. If the original value parameter is specified, the constructor creates and returns a wrapper object for the original values, that is, a number object, a Boolean object, or a string object. When you use the object () constructor as a function call without using the new operator, it behaves the same as when you use the new operator.

Property
Constructor
A reference to a JavaScript function that is the constructor of an object

Method

1.hasOwnProperty ()
Checks whether the object has a locally defined (not inherited), property with a specific name.

Copy Code code 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 ()

Grammar
Object.isprototypeof (o)

Parameters
O
Any object.


return value
Returns true if object is a prototype of O. Returns False if O is not an object, or if object is not a prototype of O.

Describe
The JavaScript object inherits the properties of the prototype object. The prototype of an object is referenced by the prototype property that is used to create and initialize the constructor of the object. The isPrototypeOf () method provides a way to determine whether an object is a prototype of another object. This method can be used to determine the class of an object.

Example

Copy Code code 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,3] is an array
Here's another way to perform the same test
(O.constructor = = Object); True:o is created with Object () constructor
(O.tostring.constructor = = Function); True:o.tostring is a function
/prototype object itself to the prototype object. The following call returns True
Describes the functions following the Function.prototype and Object.prototyp properties.
Object.prototype.isPrototypeOf (Function.prototype);

3.ProertyIsEnumerable ()

Grammar
Object.propertyisenumerable (propname)

Parameters
PropName
A string that contains the name of the object prototype.

return value
Returns true if object has a PropName property named "," and the property is enumerable (that is, it can be enumerated with a for/in loop).

Describe
You can use the For/in statement to traverse an object "enumerable" property. But not-all of the attributes of an object are enumerable, and the properties added to the object through JavaScript code are enumerable, while the predefined properties of the inner object, such as methods, are usually not enumerable. The Propertylsenumerable () method provides a way to differentiate between enumerable and enumerable properties. Note, however, that the ECMAScript standard stipulates that the propertyIsEnumerable () method does not detect the prototype chain, which means that it applies only to local properties of objects and cannot detect the enumerable of inherited properties.

Example

Copy Code code as follows:

var o = new Object (); Create an Object
o.x = 3.14; Definition-a property
O.propertyisenumerable ("X"); True Property x is a local, enumerable
O.propertyisenumerable ("Y"); False:o No attribute y
O.propertyisenumerable ("toString"); The False:tostrlng property is inherited
Object.prototype.propertyIsEnumerable ("toString"); False: Enumeration of

Bugs

When the standard limit propertylsenumerable () method can detect only inheritable properties, it is clearly wrong. Internet Explorer 5.5 implements this method by standard. The NESTACPE 6.0 implementation of the propertyIsEnumerable () method takes into account the prototype chain. Although this approach is preferable, it conflicts with the standard, so Netscape 6.1 modified it to match IE 5.5. Because of this error in the standard, this method is not so useful.

Copy Code code 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's a way
******************************

Copy Code code as follows:

<script language= "JavaScript" >
Function Object (value,a,b) {
This.title = value;
This.funb = function () {
THIS.A = A;
this.b = b;
alert (A+B);
}
}
var obj = new Object ("AAA", 1,2);
alert (obj.title);
Obj.funb ();
This adds a new method to the object
Object.prototype.name = "123456";
alert (obj.name);
</script>

This is another way

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.