持续完善 Pinda.cn 秒建营销活动 通过低代码 零代码的模式快速创建营销活动,欢迎使用 。

prototype and constrator


prototype:
用 prototype 属性提供对象的类的一组基本功能。 对象的新实例“继承”赋予该对象原型的操作。

例如,要为 Array 对象添加返回数组中最大元素值的方法。 要完成这一点,声明该函数,将它加入 Array.prototype, 并使用它。

function array_max( ){
   var i, max = this[0];
   for (i = 1; i < this.length; i++)
   {
   if (max < this[i])
   max = this[i];
   }
   return max;
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = x.max( );
该代码执行后,y 保存数组 x 中的最大值,或说 6


constructor:

constructor 属性是所有具有 prototype 的对象的成员。它们包括除 Global 和 Math 对象以外的所有 JScript 固有对象。constructor 属性保存了对构造特定对象实例的函数的引用。 例如:

x = new String("Hi");
if (x.constructor == String)
      // 进行处理(条件为真)。

function MyFunc {
   // 函数体。
}

y = new MyFunc;
if (y.constructor == MyFunc)
      // 进行处理(条件为真)。
-------------------------------------------------------------------------------
constructor 属性
表示创建对象的函数。

object.constructor

必需的 object是对象或函数的名称。

说明
constructor 属性是所有具有 prototype 的对象的成员。它们包括除 Global 和 Math 对象以外的所有 JScript 固有对象。constructor 属性保存了对构造特定对象实例的函数的引用。 例如:

x = new String("Hi");
if (x.constructor == String)
      // 进行处理(条件为真)。

function MyFunc {
   // 函数体。
}

y = new MyFunc;
if (y.constructor == MyFunc)
      // 进行处理(条件为真)。

/******************************888/

prototype 属性
返回对象类型原型的引用。

objectName.prototype

objectName 参数是对象的名称。

说明
用 prototype 属性提供对象的类的一组基本功能。 对象的新实例“继承”赋予该对象原型的操作。

例如,要为 Array 对象添加返回数组中最大元素值的方法。 要完成这一点,声明该函数,将它加入 Array.prototype, 并使用它。

function array_max( ){
   var i, max = this[0];
   for (i = 1; i < this.length; i++)
   {
   if (max < this[i])
   max = this[i];
   }
   return max;
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = x.max( );
该代码执行后,y 保存数组 x 中的最大值,或说 6。
所有 JScript 固有对象都有只读的 prototype 属性。可以象该例中那样为原型添加功能,但该对象不能被赋予不同的原型。然而,用户定义的对象可以被赋给新的原型。
本语言参考中每个内部对象的方法和属性列表指出哪些是对象原型的部分,哪些不是。
------------------------------------------------------------------------------------
简单的说明一下constructor和prototype


<script type="text/javascript">
//&lt;![CDATA[
    function jsclass(fValue) {
      this.p = fValue+"blog.never-online.net";
      this.write = function () {
        document.write(this.p);
      }
    }
    var a = {};
    a.constructor = jsclass;
    a.prototype = jsclass.prototype;
    a.constructor('http://');
    a.write();
//]]&gt;
</script>

posted @ 2006-10-29 21:47  工具人Kim哥  阅读(371)  评论(0编辑  收藏  举报
持续完善 Pinda.cn 秒建营销活动 通过低代码 零代码的模式快速创建营销活动,欢迎使用 。