Summary of JavaScript knowledge points (11): Explanation of Object classes in js, javascriptobject

Source: Internet
Author: User
Tags hasownproperty

Summary of JavaScript knowledge points (11): Explanation of Object classes in js, javascriptobject

The Object in JavaScript is the base class of all objects in JS. That is to say, all objects in JS are derived from Object objects. Object objects are mainly used to encapsulate arbitrary data into objects.

I. Object Class Introduction

The Object class is the base class (parent class) of all JavaScript classes. It provides a simple method to create custom objects and does not require programmers to define constructors.

Ii. main attributes of the Object class

1. constructor: constructor of the object.

2. prototype: Obtain the prototype object of the class, which is static.

Iii. main Object Methods

  1. hasOwnProperty (propertyName)

Determines whether an object has a specific attribute. This attribute must be specified with a string. For example, obj. hasOwnProperty ("name"), a Boolean value is returned. This method cannot check whether the property exists in the prototype chain of the object. The property must be a member of the object.

Var str = ""; alert ("str. the result of hasOwnProperty (\ "split \") is: "+ str. hasOwnProperty ("split"); // return falsealert ("String. prototype. the result of hasOwnProperty (\ "split \") is: "+ String. prototype. hasOwnProperty ("split"); // return true

Running result:

  

HasOwnProperty is used not only here. In Jquery, the indispensable step in writing plug-ins is to initialize parameters. One of the most important methods is $. extend (); its principle is to apply the hasOwnProperty () method; Use the for in loop to traverse the object members, whether there are object members with the same name, if yes, replace the old one with the new object member. In this way, we can modify the parameter changes in the method to control the process of the program, for those parts that haven't changed, we still use the default value for control. We can also simulate this extend function, as shown below:

Function extend (target, source) {// New for (var I in source) {if (target. hasOwnProperty (I) {target [I] = source [I] ;}} return target;} var a = {"first":, "second": "lyl ", "third": "bob"}; var B = {"third": "leo"}; extend (a, B); for (var I in) {alert (a [I]); // It was originally bob and now it is leo}

 2. isPrototypeOf (object)

Determine whether the object is a prototype of another object.

Obj1.isPrototypeOf (obj2 );

Obj1 is an instance of an object, and obj2 is another object that will check its prototype chain. The prototype chain can be used to share functions between different instances of the same object type. If the prototype chain of obj2 contains obj1, The isPrototypeOf method returns true. If obj2 is not an object or obj1 does not appear in the prototype chain of obj2, The isPrototypeOf method returns false.

<Script type = "text/javascript"> function foo () {this. name = 'foo';} function bar () {} bar. prototype = new foo (); var goo = new bar (); alert (goo. name); // fooalert (bar. prototype. isPrototypeOf (goo); // true, if the current object goo exists in the bar prototype chain, the isPrototypeOf method returns true </script>

  3. propertyIsEnumerable (propertyName)

Through this method, we can check whether the object member can be traversed. If it can be traversed, it proves that this object can be traversed using the for in loop,

Format: obj. propertyIsEnumerable (propertyName)

If propertyName exists in obj, you can use a... If the In loop is exhausted, the propertyIsEnumerable attribute returns true. If the object does not have a specified attribute or the specified attribute is not enumerable, The propertyIsEnumerable attribute returns false. Typically, predefined attributes are not configurable, and user-defined attributes are always configurable.

  4. toString (): returns the string corresponding to the object.

  5. valueOf (): returns the original type of the object.

The preceding five methods are defined on Object. prototype. All objects in ECMAScript are inherited from objects. Therefore, all objects in ECMAScript have several methods.

Test code 1:

Var p = new Object (); // directly create an Object through the Object // dynamically add attribute p to the p Object. age =; p. name = "lone wolf"; // extends the Object class and adds a Show method Object to the Object class. prototype. show = function () {alert (this. age + "\ t" + this. name);} alert (p. age); p. show (); document. write ("<pre>"); document. writeln ("p. constructor: "+ p. constructor); // obtain the object's constructor document. writeln ("Object. prototype: "+ Object. prototype); // obtain the prototype object. prototype is a static attribute and can only be passed through the class name. prototype "access document. writeln ("p. isPrototypeOf (p): "+ p. isPrototypeOf (p); document. writeln ("p. hasOwnProperty (\ "Age \"): "+ p. hasOwnProperty ("Age"); document. writeln ("p. propertyIsEnumerable (\ "Age \"): "+ p. propertyIsEnumerable ("Age"); document. writeln ("p. toString (): "+ p. toString (); document. writeln ("p. valueOf (): "+ p. valueOf (); document. write ("</pre> ");

Running result:

Test code 2:

Var Car = function () {}; Car. prototype. hello = function () {alert ("hello car") ;}; var car = new Car (); car. f = function () {alert ("custom method");} document. write ("<pre>"); document. writeln ("car. the result of hasOwnProperty (\ "f \") is: "+ car. hasOwnProperty ("f"); // ture, the car object has the f method document. writeln ("car. the result of propertyIsEnumerable (\ "f \") is: "+ car. propertyIsEnumerable ("f"); // ture, the car object has the f method, and the f method is the document that can be enumerated. writeln ("car. hasOwnProperty (\ "hello \") "+ car. hasOwnProperty ("hello"); // false, because the car itself does not have the "hello" method document. writeln ("car. the result of propertyIsEnumerable (\ "hello \") is: "+ car. propertyIsEnumerable ("hello"); // false. document cannot be enumerated without this method. writeln ("car. constructor. prototype. the result of hasOwnProperty (\ "hello \") is: "+ car. constructor. prototype. hasOwnProperty ("hello"); // true, the car-like prototype has the hello method document. writeln ("car. constructor. prototype. the result of propertyIsEnumerable (\ "hello \") is: "+ car. constructor. prototype. propertyIsEnumerable ("hello"); // true, the car prototype hello method of the Car class can be enumerated document. writeln ("Car. prototype. the result of hasOwnProperty (\ "hello \") is: "+ Car. prototype. hasOwnProperty ("hello"); // true, the car-like prototype has the hello method document. writeln ("Car. prototype. the result of propertyIsEnumerable (\ "hello \") is: "+ Car. prototype. propertyIsEnumerable ("hello"); document. write ("</pre> ");

Running result:

  

The above is a detailed description of the Object class in JavaScript Summary (11). I hope it will help you.

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.