1.

 <script type="text/javascript">

     // Define the Person constructor
var Person = function(firstName) {
this.firstName = firstName;
}; // Making sure that this points to the right thing regardless of how the object is instantiated can be difficult. However, there is a simple idiom to make this easier.
var Person = function(firstName) {
if (this instanceof Person) {
this.firstName = firstName;
} else {
return new Person(firstName);
}
} // Add a couple of methods to Person.prototype
Person.prototype.walk = function(){
console.log("I am walking!");
}; Person.prototype.sayHello = function(){
console.log("Hello, I'm " + this.firstName);
}; // Define the Student constructor
function Student(firstName, subject) {
// Call the parent constructor, making sure (using Function#call)
// that "this" is set correctly during the call
Person.call(this, firstName); // Initialize our Student-specific properties
this.subject = subject;
} // Create a Student.prototype object that inherits from Person.prototype.
// Note: A common error here is to use "new Person()" to create the
// Student.prototype. That's incorrect for several reasons, not least
// that we don't have anything to give Person for the "firstName"
// argument. The correct place to call Person is above, where we call
// it from Student.
Student.prototype = Object.create(Person.prototype); // See note below // Set the "constructor" property to refer to Student
Student.prototype.constructor = Student; // Replace the "sayHello" method
Student.prototype.sayHello = function(){
console.log("Hello, I'm " + this.firstName + ". I'm studying "
+ this.subject + ".");
}; // Add a "sayGoodBye" method
Student.prototype.sayGoodBye = function(){
console.log("Goodbye!");
}; // Example usage:
var student1 = new Student("Janet", "Applied Physics");
student1.sayHello(); // "Hello, I'm Janet. I'm studying Applied Physics."
student1.walk(); // "I am walking!"
student1.sayGoodBye(); // "Goodbye!" // Check that instanceof works correctly
console.log(student1 instanceof Person); // true
console.log(student1 instanceof Student); // true // 如果 On older JavaScript engines without Object.create, one can either use a "polyfill" (aka "shim", see the linked article), or one can use a function that achieves the same result, such as:
function createObject(proto) {
function ctor() { }
ctor.prototype = proto;
return new ctor();
} // Usage:
Student.prototype = createObject(Person.prototype);

面向对象的JavaScript-002的更多相关文章

  1. 前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型

    前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型 前言(题外话): 有人说拖延症是一个绝症,哎呀治不好了.先不说这是一个每个人都多多少少会有的,也不管它究竟对生活有多么大的 ...

  2. 前端开发:面向对象与javascript中的面向对象实现(一)

    前端开发:面向对象与javascript中的面向对象实现(一) 前言: 人生在世,这找不到对象是万万不行的.咱们生活中,找不到对象要挨骂,代码里也一样.朋友问我说:“嘿,在干嘛呢......”,我:“ ...

  3. 面向对象的 JavaScript

    面向对象的javascript 一.创建对象 创建对象的几种方式: var obj = {}; var obj = new Object(); var obj = Object.create(fath ...

  4. 摘抄--全面理解面向对象的 JavaScript

    全面理解面向对象的 JavaScript JavaScript 函数式脚本语言特性以及其看似随意的编写风格,导致长期以来人们对这一门语言的误解,即认为 JavaScript 不是一门面向对象的语言,或 ...

  5. 面向对象的JavaScript --- 动态类型语言

    面向对象的JavaScript --- 动态类型语言 动态类型语言与面向接口编程 JavaScript 没有提供传统面向对象语言中的类式继承,而是通过原型委托的方式来实现对象与对象之间的继承. Jav ...

  6. 面向对象的JavaScript --- 封装

    面向对象的JavaScript --- 封装 封装 封装的目的是将信息隐藏.一般而言,我们讨论的封装是封装数据和封装实现.真正的封装为更广义的封装,不仅包括封装数据和封装实现,还包括封装类型和封装变化 ...

  7. 面向对象的JavaScript --- 多态

    面向对象的JavaScript --- 多态 多态 "多态"一词源于希腊文 polymorphism,拆开来看是poly(复数)+ morph(形态)+ism,从字面上我们可以理解 ...

  8. 面向对象的JavaScript --- 原型模式和基于原型继承的JavaScript对象系统

    面向对象的JavaScript --- 原型模式和基于原型继承的JavaScript对象系统 原型模式和基于原型继承的JavaScript对象系统 在 Brendan Eich 为 JavaScrip ...

  9. 第1章 面向对象的JavaScript

    针对基础知识的每一个小点,我都写了一些小例子,https://github.com/huyanluanyu1989/DesignPatterns.git,便于大家理解,如有疑问,大家可留言给我,最近工 ...

  10. javascript面向对象之Javascript 继承

    转自原文javascript面向对象之Javascript 继承 在JavaScript中实现继承可以有多种方法,下面说两种常见的. 一,call 继承 先定义一个“人”类 //人类 Person=f ...

随机推荐

  1. Ubuntu 14.04 配置安卓5.1编译环境

    Ubuntu 14.04版本 电脑cpu必须是64位 硬盘分配大约100G的空间 1.ubuntu中更新源 $ sudo apt-get update 2.android5.1需要安装openjdk- ...

  2. jmetr _MD5加密_获取签名

    要达到的目的: app每个请求里面 请求头都带有一个 sign 的参数, 他的值是通过 开发自己设计的拼接方式 再通过md5加密生成 我们就是要生成这个sign的值出来 准备: 和开发要到签名组成公式 ...

  3. easyui datagrid 格式化列显示两位小数、千分位

    { field: , formatter: function (value, row, index) { if (row != null) { ); } } }, //二位小数.千分位 { field ...

  4. H3C V7版本的系统默认权限

    H3C (v7平台)Console口通过账号密码登陆配置教程 http://www.023wg.com/h3c/496.html H3C (v7平台)Console口通过账号密码登陆配置教程 看不懂的 ...

  5. Jq将字符串复制粘贴到剪贴板

      第一种: 自己测试时 只适合于input 和textarea 但是针对于其他标签的复制就不能用了.代码如下: <!DOCTYPE html> <html> <head ...

  6. vue-router规则下 history模式在iis服务器上配置

    vue默认模式是hash模式    url地址栏会带有“#”这个字符. 例如:http://www.xxx.com/#/index 感觉和正常的url相比有点丑. 如何让此地址如正常的url一样  官 ...

  7. setTimeout和setInterval的unref()和ref()用法

    var testFunction=function(){ console.log("guoyansi"); } var timer=setInterval(testFunction ...

  8. RAC的时间同步问题

    今天在两个节点上面安装RAC,在安装clusterware的时候OUI总是提示失败.查到资料的得知: 特此记录: 需要在在所有的集群节点上设置正确的日期和时间 在安装 Oracle 集群件.数据库以 ...

  9. Android:自定义Sqlite数据库路径

    默认的sqlite数据库是放在/data/data/database目录下的,今天看腾讯云IM的demo发现再该路径下找不到它存放消息的数据库,找了下后发现居然是放在/data/data/files目 ...

  10. ubuntu下安装和配置最新版JDK8傻瓜教程

    ubuntu下安装和配置最新版JDK8傻瓜教程 听语音 | 浏览:18940 | 更新:2014-07-14 22:13 | 标签:ubuntu 1 2 3 4 5 6 分步阅读 ubuntu系统通常 ...