javascript学习笔记——objects and arrays(3) 通用对象的属性及方法

1. 构造器属性

每个对象都有一个constructor属性,指向初始化对象的构造方法。例如,如果用Date()构造器创建了对象d,那么属性d.constructor 指向Date:

<code>

var d = new Date();

d.constructor == Date;

</code>

因为构造方法定义新的种类或对象类,所以constructor属性可以用来确定对象的类型。

<code>

if( ( typeof o == "object") && (o.constructor == Date))

// then do something with the Date object...

instanceof操作符checks 属性constructor的值,所以上边的代码也可以写为:

if( ( typeof o == "object") && ( o instanceof Date) )

// then do something with the Date object...

2. 

toString( );

toLocalString( );

valueOf( );

hasOwnProperty( );    noninherited 属性

     var o = { };

    o.hasOwnProperty("undef") ; //false

    o.hasOwnProperty(" toString"); //false

    Math.hasOwnProperty("cos"); //true

propertyIsEnumerable( );    noninherited 属性

      var o = { x:1 };

      o.propertyIsEnumerable ( "x" );  //true

      o.propertyIsEnumerable( "y" );  //false

      o.propertyIsEnumerable ( " valueOf" ); //false  : property is inherited 

isPrototypeOf( );

   var o = { };

   Object.prototype.isPrototypeOf( o );  //true: o.constructor == Object

   Object.isPrototypeOf( o );  //false

    o.isPrototypeOf ( Object.prototype ); //false

    Function.prototype.isPrototypeOf( Object) ; //true : Object.constructor == Function

</code>

 

 

 

你可能感兴趣的:(JavaScript,prototype)