面向对象的JavaScript-003
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了面向对象的JavaScript-003,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1554字,纯文字阅读大概需要3分钟。
内容图文
![面向对象的JavaScript-003](/upload/InfoBanner/zyjiaocheng/1334/978d8137b73449b5bb64d4e590a04517.jpg)
1.
1 // Since JavaScript doesn‘t exactly have sub-class objects, prototype is a useful workaround to make a “base class” object of certain functions that act as objects. For example: 2 var Person = function() { 3this.canTalk = true; 4 }; 5 6 Person.prototype.greet = function() { 7if (this.canTalk) { 8 console.log(‘Hi, I am ‘ + this.name); 9 } 10 }; 1112var Employee = function(name, title) { 13 Person.call(this); 14this.name = name; 15this.title = title; 16 }; 1718 Employee.prototype = Object.create(Person.prototype); 19 Employee.prototype.constructor = Employee; 2021 Employee.prototype.greet = function() { 22if (this.canTalk) { 23 console.log(‘Hi, I am ‘ + this.name + ‘, the ‘ + this.title); 24 } 25 }; 2627var Customer = function(name) { 28 Person.call(this); 29this.name = name; 30 }; 3132 Customer.prototype = Object.create(Person.prototype); 33 Customer.prototype.constructor = Customer; 3435var Mime = function(name) { 36 Person.call(this); 37this.name = name; 38this.canTalk = false; 39 }; 4041 Mime.prototype = Object.create(Person.prototype); 42 Mime.prototype.constructor = Mime; 4344var bob = new Employee(‘Bob‘, ‘Builder‘); 45var joe = new Customer(‘Joe‘); 46var rg = new Employee(‘Red Green‘, ‘Handyman‘); 47var mike = new Customer(‘Mike‘); 48var mime = new Mime(‘Mime‘); 4950 bob.greet(); 51// Hi, I am Bob, the Builder5253 joe.greet(); 54// Hi, I am Joe5556 rg.greet(); 57// Hi, I am Red Green, the Handyman5859 mike.greet(); 60// Hi, I am Mike6162 mime.greet();
原文:http://www.cnblogs.com/shamgod/p/5523313.html
内容总结
以上是互联网集市为您收集整理的面向对象的JavaScript-003全部内容,希望文章能够帮你解决面向对象的JavaScript-003所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。