javascript模拟继承
2014-03-06 20:38
417 查看
js 本没有继承,只用js语法来实现继承效果 function Person(age, name) { this.name =name; this.age = age; } Person.prototype.sayHi = function () { alert("age="+this.age+",name"+this.name); } var p1 = new Person(12, "ff"); var p2 = new Person(21, "fffs"); alert(p1.sayHi == p2.sayHi); function Father(name, age) { this.name = name; this.age = age; } Father.prototype.sayHi = function () { alert(this.name+","+this.age); } function Son(name,age,gender) { this.gender = gender; //call调用自己本身 Father.call(this, name, age);//"继承" "父类"的成员 } // Son.prototype = Father.prototype; //可以用父类的方法(这里有指针的问题) Son.prototype = new Father(); //得到父类对象 Son.prototyp.fungame = function () { }; //prototype也是一个对象 var f1 = new Father("JamesZou", 29); f1.sayHi(); var s1 = new Son("jameszou", 1, true);
相关文章推荐
- Javascript本地存储数据方案
- 设为首页 加入收藏的js代码
- javascript匿名函数
- JavaScript权威指南:数组(第七章)
- js网页版计算器的简单实现
- 视图和视图解析器---15.4.8: 输出JSON
- 视图和视图解析器---15.4.3: JSP和JSTL
- 【marks】js素材
- [ javascript ] 一切皆为闭包!
- 2013年人气最高的JavaScript框架排名
- grunt中ejs-static配置
- 解析json并存入对象,然后逐行打印对象的属性
- Extjs学习笔记--(五,事件)
- Javascript函数
- Javascript变量
- 开发阶段Jetty运行Jsp报错且响应空白
- javaweb之response随机生成图片验证码加载到web页面中(本实例使用了禁止浏览器缓存头),并且单击使用javascript换图片
- action中json数据的运用举例
- JavaScript正则
- json数据格式