Constructor
new Object()
new Object(value)
parameter
value
Optional parameter that declares the original value (i.e., number, boolean value, or string) to be converted into a Number object, Boolean object, or String object. This object is not supported by JavaScript prior to 1.1 and ECMAScript Vl.
Return value
If no value parameter is passed to the constructor, it will return a newly created Object instance. If the original value parameter is specified, the constructor creates and returns the original value wrapper object, namely the Number object, the Boolean object, or the String object. When the new operator is not used, the Object() constructor is called as a function, it behaves the same as when using the new operator.
property
constructor
Reference to a JavaScript function that is the object's constructor
method
1.hasOwnProperty( )
Check whether the object has locally defined (non-inherited) properties with specific names.
The code copy 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()
grammar
object.isPrototypeOf(o)
parameter
o
Any object.
Return value
Return true if object is a prototype of O. If o is not an object, or object is not a prototype of o, then false is returned.
describe
JavaScript objects inherit the properties of the prototype object. An object's 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 an object.
Example
The code copy 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,3] is an array
//The following is another way to perform the same test
(o.constructor == Object); // true: o was created with Object( ) constructor
(o.toString.constructor == Function); // true: o.toString is a function
/Prototype The object itself is the prototype object. The following call returns true
//Instructions the function inherits the Function.prototype and Object.prototype properties.
Object.prototype.isPrototypeOf(Function.prototype);
3.ProertyIsEnumerable()
grammar
object.propertyIsEnumerable(propname)
parameter
propname
A string containing the name of the object prototype.
Return value
Return true if the object has a non-inherited property named propname and the property is enumerable (i.e., it can be enumerated with a for/in loop).
describe
Use the for/in statement to traverse the properties of an object "enumerable" . But not all properties of an object are enumerable. Attributes added to an object through JavaScript code are enumerable, while predefined properties (such as methods) of internal objects are usually not enumerable. The propertiesEnumerable() method provides a way to distinguish between enumable and non-enumerable properties. But it should be noted that the ECMAScript standard stipulates that the propertyIsEnumerable() method does not detect the prototype chain, which means that it only applies to the local properties of the object and cannot detect the enumeration of inherited properties.
Example
The code copy is as follows:
var o = new Object( ); // Create an object
ox = 3.14; // Define - attributes
o.propertyIsEnumerable("x"); // true property x is local and enumerable
o.propertyIsEnumerable("y"); //false: o has no attribute y
o.propertyIsEnumerable("toString"); //false: the toStrlng property is inherited
Object.prototype.propertyIsEnumerable("toString"); // false: enumerated
Bug
It is obviously wrong when the standard limits propertiesEnumerable() method to detect only non-inherited properties. Internet Explorer 5.5 implements this method according to the standard. The propertyIsEnumerable() method implemented in Nestacpe 6.0 takes into account the prototype chain. While this approach is desirable, it conflicts with the standard, so Netscape 6.1 modified it to match IE 5.5. Since there is this error in the standard, this method is not that useful.
The code copy 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
************************************
The code copy is 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();
//Add a new method to the object here
object.prototype.name = "123456";
alert(obj.name);
</script>
Here is another way