关于原型和继承的问题。

malpower 2011-01-14 10:44:50
刚开始学javascript,遇到了一个问题。
指定了对象的prototype属性后,没有效果,就像没有制定一样。
代码如下,望达人解疑。

sd.htm:

<script type="text/javascript" src="aa.js">

</script>

<input type="button" onClick="alert(bb.getYear());" value="jjjj">



aa.js:

function nn()
{
var jj={
name: "malpower",
age: 15,
prototype: new Date(),
school: "天堂",
show: function(){window.alert("I am "+this.name+","+this.age+" 岁,就读于 "+this.school+" "+this.prototype.getYear());}
};

return jj;
}
var bb=new nn();
...全文
78 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
JKelfin 2011-01-14
  • 打赏
  • 举报
回复
给你一个我经常用来写面向对象的JS的方法
<html>
<script>
var Util = {};
Util.defineClass = function(data)
{
var classname = data.name;
var superclass = data.extend || Object;
var constructor = data.construct || function(){};
var methods = data.methods || {};
var statics = data.statics || {};

// create the prototype object that will become prototype for the result class
var proto = new superclass();

for(var p in proto)
{
// delete any noninherited properties
if(proto.hasOwnProperty(p))
{
delete proto[p];
}
}

// copy instance methods to the prototype object
for(var p in methods)
{
proto[p] = methods[p];
}

proto.constructor = constructor;
proto.superclass = superclass;
if(classname)
{
proto.classname = classname;
}

// associate the prototype object with the constructor function
constructor.prototype = proto;

// copy static properties to the constructor
for(var p in statics)
{
constructor[p] = statics[p];
}

return constructor;
};

var ClassSums = {};
ClassSums.SupperClass = Util.defineClass(
{
name : 'SupperClass',//类名
construct: function(name)
{
this.name = name;
},

statics :
{
//静态变量
STATIC_PARA : "father STATIC_PARA",
//静态方法
staticMethod: function()
{
//打印静态变量
console.log(ClassSums.SupperClass.STATIC_PARA);
}
},

methods :
{
instance_para : "instance_para",

//实例变量
instentMethod : function()
{
console.log("father static : " + ClassSums.SupperClass.STATIC_PARA);
console.log("father priva1 : " + this.name);
console.log("father priva2 : " + this.instance_para);
}
}
});

ClassSums.ChildClass = Util.defineClass(
{
name : 'SupperClass',//类名
extend: ClassSums.SupperClass,//继承
});

function init()
{
var father = new ClassSums.SupperClass("father");
var child = new ClassSums.SupperClass("child");

var button1 = document.getElementById("button1");
var button2 = document.getElementById("button2");
var button3 = document.getElementById("button3");
var button4 = document.getElementById("button4");

button1.onclick = function(){father.staticMethod();};
button2.onclick = function(){father.instentMethod();};
button3.onclick = function(){child.staticMethod();};
button4.onclick = function(){child.instentMethod();};
}
</script>
<body onload="init()">
<input type="button" id="button1" value="button1"/>
<input type="button" id="button2" value="button2"/>
<input type="button" id="button3" value="button3"/>
<input type="button" id="button4" value="button4"/>
</body>
</html>
KK3K2005 2011-01-14
  • 打赏
  • 举报
回复
路还没走稳就跑步那
Mr-Jee 2011-01-14
  • 打赏
  • 举报
回复
var jj={
name: "malpower",
age: 15,
prototype: new Date(),
school: "天堂",
show: function(){window.alert("I am "+this.name+","+this.age+" 岁,就读于 "+this.school+" "+this.prototype.getYear());}
};
这样写和原型链继承没一毛钱关系了
仲兴轩 2011-01-14
  • 打赏
  • 举报
回复
bb.prototype.getYear();


你这里都会这么用,
show: function(){window.alert("I am "+this.name+","+this.age+" 岁,就读于 "+this.school+" "+this.prototype.getYear());}
kaifadi 2011-01-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jkelfin 的回复:]

给你一个我经常用来写面向对象的JS的方法
<html>
<script>
var Util = {};
Util.defineClass = function(data)
{
var classname = data.name;
var superclass = data.extend || Object;
var constructor = data.construct || ……
[/Quote]

如果楼主的上面的基本代码都很难搞清楚,那你这个代码,他要完全看懂估计也异常吃力!不如先不管复杂的东西,死记一点定义动态属性原型方法的方式,这种用方式用得最普遍,只记这一点就OK了。其他定义方式先不管!什么字面量模式之类的概念,用久了看多了代码,自然会慢慢明白!
xiaeelee 2011-01-14
  • 打赏
  • 举报
回复
首先js里的类依然是用new来实例化的,但是,作为构造器的js函数是不需要return语句的,你加了return语句,new操作符就不是实例化类,再有prototype是给函数加原型对象的,你把他加到对象上是什么意思。这么不伦不类的代码,建议你还是多看看手册吧

87,923

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧