JS学习笔记03-初识继承
2014-06-01 16:23
477 查看
<!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); console.log(btn.hasOwnProperty('title'));//true console.log(btn.hasOwnProperty('setTitle'));//false console.log(toolbar.hasOwnProperty('title'));//true console.log(toolbar.hasOwnProperty('setTitle'));//true </script> </body>
相关文章推荐
- JavaScript高级程序设计(第2版) 学习笔记:(六)js基于构造函数与原型的继承
- 【js学习笔记-031】--继承
- KO学习笔记之初识Knockout.js
- 2016.06.15廖雪峰JS__学习笔记(原型继承)__P9
- js学习笔记之继承
- JavaScript高级程序设计(第3版)学习笔记6 初识js对象
- HTML5学习笔记(十六):原型、类和继承【JS核心知识点】
- js学习笔记:对象——继承
- 【node.js学习笔记】第一章 初识nodejs
- javascript继承学习系列之一:初识JS的OOP
- JS继承学习笔记1
- JS高级程序设计学习笔记——继承
- JavaScript高级程序设计(第3版)学习笔记6 初识js对象
- 【09-23】js原型继承学习笔记
- Javascript学习笔记03——Js基本概念
- javascript学习笔记(十) js对象 继承
- 【JS复习笔记】03 继承
- 【学习笔记六】 - js中 创建对象的模式与继承 及 js中实现块级作用域和函数私有变量 《js高程》6-7笔记
- Learn Objective‐C,学习笔记03 类别(实现多重继承的方法)
- JavaWeb学习笔记03-js的String对象