刚开始学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();
指定了对象的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();
你这里都会这么用,
show: function(){window.alert("I am "+this.name+","+this.age+" 岁,就读于 "+this.school+" "+this.prototype.getYear());}
name: "malpower",
age: 15,
prototype: new Date(),
school: "天堂",
show: function(){window.alert("I am "+this.name+","+this.age+" 岁,就读于 "+this.school+" "+this.prototype.getYear());}
};
这样写和原型链继承没一毛钱关系了
<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>