日期:2014-05-16 浏览次数:20479 次
function Class1() { this.name = "class1"; this.showNam = function() { alert(this.name); } } function Class2() { this.name = "class2"; } var c1 = new Class1(); var c2 = new Class2(); c1.showNam.call(c2);弹出class2
<script> function Class1() { var test = "yellow"; this.showTxt = function(txt) { alert(test); } } function Class2() { Class1.call(this); this.showTxt = function() { alert("test"); } } var c2 = new Class2(); c2.showTxt("cc"); </script>这里就是输出test
<script> function ClassA(c) { this.color = c; } ClassA.prototype.sayColor = function () { alert(this.color); }; function ClassB(c, n) { ClassA.call(this, c); this.name = n; } var objB = new ClassB("red", "cxy"); objB.sayColor(); //运行报错 不存在这个方法 </script>
<script> function ClassA(c) { this.color = c; } ClassA.prototype.sayColor = function () { alert(this.color); }; function ClassB(c, n) { ClassA.call(this, c); this.name = n; } ClassB.prototype = new ClassA(); ClassB.prototype.sayName = function () { alert(this.name); }; var objA = new ClassA("blue"); var objB = new ClassB("red", "cxy"); objA.sayColor(); //输出 "blue" objB.sayColor(); //输出 "red" objB.sayName(); //输出 "cxy" </script>