Object objects in JavaScript are the base class of all objects in JS, that is, all objects in JS are derived from Object objects. Object objects are mainly used to encapsulate arbitrary data into object form.
1. Introduction to Object class
The Object class is the base class (parent class) of all JavaScript classes, providing an easy way to create custom objects without requiring programmers to define constructors.
2. Main properties of Object class
1.constructor: object constructor.
2.prototype: Obtain the prototype object of the class, static property.
3. Main methods of Object class
1.hasOwnProperty(propertyName)
Determines whether the object has a specific attribute. This property must be specified with a string, for example, obj.hasOwnProperty("name"), returning a boolean value. This method cannot check whether the object's prototype chain has this property; it must be a member of the object itself.
var str ="";alert("str.hasOwnProperty(/"split/") is: "+str.hasOwnProperty("split")); //return falsealert("String.prototype.hasOwnProperty(/"split/") is: "+String.prototype.hasOwnProperty("split")); //return trueRunning results:
The usage of hasOwnProperty is not only here. In Jquery, an indispensable step in writing plug-ins is to initialize parameters. One of the very important methods is $.extend(); its principle is to apply the hasOwnProperty() method; use for in loop to traverse object members, if there are object members of the same name, if there are, 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 have not changed, we can still use the default value to control it. 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={"first":,"second":"lyl","third":"bob"};var b={"third":"leo"};extend(a,b);for(var i in a){alert(a[i]);//Originally it was bob, but now it has become leo}2.isPrototypeOf(object)
Determine 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. Prototype chains can be used to share functionality among different instances of the same object type. If 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 will return 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 there is the current object goo in the prototype chain of bar, then the isPrototypeOf method returns true</script>3.propertyIsEnumerable(propertyName)
Through this method, we can detect whether the member of this object is traversable. If it is traversable, it proves that this object can be traversed using a for in loop.
The format is as follows: obj.propertyIsEnumerable(propertyName)
If propertyName exists in obj and can be exhaustively enumerated using a For…In loop, then the propertyIsEnumerable property returns true. If the object does not have the specified property or the specified property is not enumerable, the propertyIsEnumerable property returns false. Typically, predefined properties are not enumerable, while user-defined properties are always enumerable.
4.toString(): Return the string corresponding to the object
5.valueOf(): Returns the original type corresponding to the object
The above 5 methods are defined on Object.prototype. All objects in ECMAScript are inherited from Object, so all objects on ECMAScript have several methods.
Test code 1:
var p = new Object(); //Create an object directly through Object//Dynamic attribute p.Age=;p.Name="克到到到到" for p objects;//Extend the Object class and add a Show method Object.prototype.Show=function(){alert(this.Age+"/t"+this.Name);}alert(p.Age);p.Show();docum ent.write("<pre>");document.writeln("p.constructor:"+p.constructor);//Get the object's constructor document.writeln("Object.prototype:"+Object.prototype);//Get the prototype object, prototype is a static property, and can only be passed through the "class name .prototype" Go to access document.writeln("p.isPrototypeOf(p): "+p.isPrototypeOf(p));document.writeln("p.hasOwnProperty(/"Age/"): "+p.hasOwnProperty("Age"));document.writeln("p.property IsEnumerable(/"Age/"): "+p.propertyIsEnumerable("Age"));document.writeln("p.toString(): "+p.toString());document.writeln("p.valueOf(): "+p.valueOf());document.write("</pre>");document.write("</pre>");Running results:
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.hasOwnProperty(/"f/") is: "+car.hasOwnProperty("f"));//ture, car object has f method document.writeln("car.propertyIsEnumerable(/"f/")) is: "+car.propertyIsEnumerable("f"));//ture, car object has f method, f method is enumerable document.writeln("car.hasOwnProperty(/"hello/")"+car.hasOwnProperty("hello")); // false, because car itself does not have the hello method document.writeln("car.propertyIsEnumerable(/"hello/") result is: "+car.propertyIsEnumerable("hello")); // false, without this method, of course, the result of document.writeln("car.constructor.prototype.hasOwnProperty(/"hello/") result is: "+car.constructor.prototype.hasOwnProperty("hello")); // true, the prototype of car class Car has the hello method document.writeln("car.constructor.prototype.propertyIsEnumerable(/"hello/") and the result is: "+car.constructor.prototype.propertyIsEnumerable("hello"));// true, the prototype of car class Car's hello method is the enumerable document.writeln("Car.prototype.hasOwnProperty(/"hello/") and the result is: "+Car.prototype.hasOwnProperty("hello"));// true, the prototype of car class Car has the hello method document.writeln("Car.prototype.propertyIsEnumerable(/"hello/") The result is: "+Car.prototype.propertyIsEnumerable("hello"));document.write("</pre>");Running results:
The above is a detailed explanation of the Object class in js that the editor introduced to you, including the summary of JavaScript knowledge points (eleven). I hope it will be helpful to you.