JavaScript Knowledge Point Summary (11) of the object class in JS _javascript skills

Source: Internet
Author: User
Tags extend hasownproperty

The object in JavaScript is the base class for all objects in JS, meaning that all objects in JS are derived from object objects. Object objects are used primarily to encapsulate arbitrary data into an object form.

Introduction of Object class

The object class is the base class (parent class) of all JavaScript classes, providing a simple way to create custom objects that no longer require a programmer to define a constructor.

Ii. main properties of object class

1.constructor: The constructor of the object.

2.prototype: Get the Prototype object of class, static property.

Iii. main methods of object class

  1.hasOwnProperty (PropertyName)

Determines whether an object has a specific attribute. This property must be specified with a string, for example, Obj.hasownproperty ("name"), which returns a Boolean value. This method cannot check whether the object's prototype chain has this property, which must be a member of the object itself.

var str = "";
The result of alert ("Str.hasownproperty split\") is: "+str.hasownproperty (" split ")); return False

Run Result:

  

hasOwnProperty usage is not only here, in jquery in writing plug-ins, the one step, is the initialization of parameters, one of the most important method is $.extend (), his principle is to apply the hasOwnProperty () method; In loop traversal of an object member, there is no object member with the same name, and some words are replaced with this new object member, in this way we can modify the parameter changes in the method to control the process of the program, and for those that have not changed, still use the default value for controlling, We can also simply simulate this extend function, as follows

function Extend (target,source) {//target old source new for
(var i in source) {
if (Target.hasownproperty (i))
{ Target[i]=source[i];
}
return target;
}
var a={"A":, "second": "Lyl", "third": "Bob"};
var b={"Third": "Leo"};
Extend (a,b);
for (var i in a) {
alert (a[i]);//originally Bob, now Leo.

 2.isPrototypeOf (object)

Determines whether the object is a prototype of another object.

Obj1.isprototypeof (OBJ2);

Obj1 is an instance of an object; Obj2 is another object that will check its prototype chain. A prototype chain can be used to share functionality between different instances of the same object type. If the OBJ2 's prototype chain contains Obj1, then the isPrototypeOf method returns True. If OBJ2 is not an object or obj1 does not appear in the prototype chain in 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); Foo
alert (bar.prototype.isPrototypeOf (Goo));//true, the isPrototypeOf method returns True if there is a current object goo in the prototype chain of bar.

  3.propertyIsEnumerable (PropertyName)

In this way we can detect whether the object member is ergodic, and if it is ergodic, prove that the object can be traversed by using the for In loop,

The format is as follows: Obj.propertyisenumerable (PropertyName)

If PropertyName exists in obj and can use a for ... The propertyIsEnumerable property returns True if the in loop is a poor lift. If object does not have the specified property or the specified property is not enumerable, then the propertyIsEnumerable property returns False. Typically, predefined properties are not enumerable, and user-defined attributes can always be enumerated.

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

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

All 5 of these methods are defined on Object.prototype, and all objects in ECMAScript are inherited by object, so all objects on ECMAScript have several methods

Test Code 1:

var p = new Object ();
dynamically adding attribute p.age= to P objects directly by object creation
;
P.name= "Aloof wolf";
Extend the object class, add a show method to the object class
Object.prototype.show=function () {
alert (this. Age+ "T" +this. Name);
alert (p.age);
P.show ();
document.write ("<pre>");
Document.writeln ("P.constructor:" +p.constructor);//Get the constructor of the object
Document.writeln ("Object.prototype:" + Object.prototype);//Get prototype object, prototype is static property, only through "class name. Prototype" to 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 ());

Run 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>"); The result of Document.writeln ("Car.hasownproperty (\ f\") is: "+car.hasownproperty (" F "));//ture,car object has F method Document.writeln ("
The result of car.propertyisenumerable (\ "f\") is: "+car.propertyisenumerable (" F "));//ture,car object has F method, F method can be enumerated Document.writeln ("Car.hasownproperty" ("hello\") "+car.hasownproperty" ("Hello")); False, because the car itself does not have the Hello method Document.writeln (car.propertyisenumerable (\ hello\) result is: "+car.propertyisenumerable (" Hello ")); False, without this method of course cannot enumerate Document.writeln ("Car.constructor.prototype.hasOwnProperty" (\ hello\) The result is: "+ Car.constructor.prototype.hasOwnProperty ("Hello"));/The prototype of the class car of True,car has the Hello method Document.writeln (" The result of car.constructor.prototype.propertyIsEnumerable (\ "Hello\") is: "+car.constructor.prototype.propertyisenumerable ("Hello")); /True, the car's prototype Hello method of the car's class is the Document.writeln ("Car.prototype.hasOwnProperty" hello\) that can be enumerated: "+car. Prototype.hasownproperty ("Hello")); the prototype of the class car of the//True,car has the Hello method Document.writeln ("
The result of Car.prototype.propertyIsEnumerable (\ "Hello\") is: "+car.prototype.propertyisenumerable (" Hello "));  document.write ("</pre>");

Run Result:

  

The above is a small series to introduce the JavaScript Knowledge Point Summary (11) of the object class in JS, I hope to 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.