首页 > 代码库 > JS学习笔记03-初识继承
JS学习笔记03-初识继承
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>test</title> <style type="text/css"> </style> </head> <body> <script type="text/javascript"> // ◆prototype方式 var Widget = function(){ title = ‘‘; this.getTitle=function(){ return this.title; } this.setTitle=function(){ this.title = arguments[0]; } } var Button = function(){} Button.prototype = new Widget(); var Edit = function(){} Edit.prototype = new Widget(); var btn = new Button(); btn.setTitle(‘button title name‘); var edt = new Edit(); edt.setTitle(‘edit title name‘); //当前对象中找不到时,查找prototype属性 console.log(edt.getTitle()); console.log(btn.getTitle()); console.log(btn.title+‘ ‘+edt.title);
// ◆call+prototype方式 var Toolbar = function(){ Widget.call(this); Toolbar.prototype = new Widget(); this.setTitle(arguments[0]); } var toolbar = new Toolbar(‘toolbar title name‘); console.log(toolbar.getTitle() +‘ ‘+ toolbar.title); </script> </body>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。