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>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。