日期:2014-05-16  浏览次数:20288 次

js类、实例对象

一 、js类

1) 定义:


function cP(id,name){
???
??? this.id = id;
??? this.name = name;
??? this.method = function(){alert("hello");};
}

2) 给cP类添加属性:


cP.prototype.method1 = function){alert("this is another method of cP");};

3) cP类调用:


var v = new cP(3,"jack");



二、js实例对象

1) 定义


? 1.1) var o = {
???????????? i:4,
???????????? method:function(){alert("hello");}
??????? };


?1.2) var o = new Object();
??????????? o.i = 4;
??????????? o.method = function(){alert("hello");
????? };


?1.3) var o = {};
??????????? o.i = 4;
??????????? o.method = function(){alert("hello");
????? };

2) 调用


o.method();