javascript——原型实现继承

来源:互联网 发布:自学软件开发 编辑:程序博客网 时间:2024/05/21 09:55
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script charset="UTF-8" type="text/javascript">/** * 1、定义两个构造函数,分别视为子类和父类 * 2、在父类的原型中添加一些属性和方法 * 3、编码后,创建一个子类实例,看能否得到父类的属性和使用父类方法 */function Parent(name,age){this.name = name;this.age = age;}Parent.prototype = {constructor:Parent,sayName:function(){alert('我是父类的方法');}}function Child(){}Child.prototype = new Parent('leo');//这里就是将子类原型和父类的原型关联起来了var c1 = new Child();alert(c1.name);//leoc1.sayName();//我是父类的方法</script></head><body></body></html>

0 0
原创粉丝点击