Javascript Object objects learn notes

  • 2020-05-05 10:56:19
  • OfStack

constructor
new Object ()

new Object(value)


parameters value
An optional parameter that declares the original value (that is, a number, Boolean, or string) to be converted to an Number object, an Boolean object, or an String object. Prior versions of JavaScript 1.1 and ECMAScript Vl do not support this object.

returns the value

If no value argument 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 a wrapper object with the original value, the Number object, Boolean object, or String object. When the Object() constructor is called as a function without the new operator, it behaves the same as when the new operator is used.

attribute
constructor
A reference to an JavaScript function, which is the constructor of the object

method

1.hasOwnProperty( )
Checks if the object has locally defined (non-inherited) properties with a specific name.


    <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.


returns the value
If object is a prototype of O, true is returned. false is returned if o is not an object or object is not the original type of o.

describes
The JavaScript object inherits the properties of the prototype object. The prototype of an object is referenced by the prototype attribute 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 the object.

example


var o = new Object(  );                          // Create an object
Object.prototype.isPrototypeOf(o)                // true: o It's an object
Function.prototype.isPrototypeOf(o.toString);    // true: toString It's a function
Array.prototype.isPrototypeOf([1,2,3]);          // true: [1,2,3] It's an array
// Here 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
/ The prototype is the object itself. The following call returns true
// Declarative function continuation Function.prototype and Object.prototyp attribute .
Object.prototype.isPrototypeOf(Function.prototype);

3.ProertyIsEnumerable()

Grammar
object.propertyIsEnumerable(propname)

Parameter
propname
A string containing the name of the object prototype.

The return value
object returns true if object has a non-inherited property named propname and that property is enumerable (it can be enumerated with the for/in loop).

Describe
The for/in statement lets you iterate over the enumerable properties of an object. But not all properties of an object are enumerable. Properties added to an object by JavaScript code are enumerable, while predefined properties (such as methods) of internal objects are generally not enumerable. The propertylsEnumerable() method provides a way to distinguish enumerable from unenumerable properties. Note, however, that the ECMAScript standard states that the propertyIsEnumerable() method does not detect the stereotype chain, meaning that it applies only to local properties of an object and cannot detect the enumerability of inherited properties.

Sample


var o = new Object(  );                // Create an object
o.x = 3.14;                            // Define a property
o.propertyIsEnumerable("x");           // true attribute x Is local and enumerable
o.propertyIsEnumerable("y");           //false : o There is no attribute y
o.propertyIsEnumerable("toString");    //false : toStrlng Properties are inherited
Object.prototype.propertyIsEnumerable("toString");  // false: The enumeration of

Bug

When the standard restricts the propertylsEnumerable() method to detecting only non-inherited properties, it is clearly wrong. Internet Explorer 5.5 implements this approach as a standard. The propertyIsEnumerable() approach implemented by Nestacpe 6.0 takes into account the prototype chain. While this approach is desirable, it conflicts with the standard, so Netscape 6.1 has modified it to match IE 5.5. Because of this error in the standard, this method is not very useful.


<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 one method,
******************************


<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();
// Here you add a new method to the object
object.prototype.name = "123456";
alert(obj.name);
</script>

This is another way to do


Related articles: