1.

 // Below is an example of how to use Object.create() to achieve classical inheritance. This is for single inheritance, which is all that JavaScript supports.
// Shape - superclass
function Shape() {
this.x = 0;
this.y = 0;
} // superclass method
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
console.info('Shape moved.');
}; // Rectangle - subclass
function Rectangle() {
Shape.call(this); // call super constructor.
} // subclass extends superclass
Rectangle.prototype = Object.create(Shape.prototype);
Rectangle.prototype.constructor = Rectangle; var rect = new Rectangle(); console.log('Is rect an instance of Rectangle?', rect instanceof Rectangle);// true
console.log('Is rect an instance of Shape?', rect instanceof Shape);// true
rect.move(1, 1); // Outputs, 'Shape moved.' // If you wish to inherit from multiple objects, then mixins are a possibility.
// The mixin function would copy the functions from the superclass prototype to the subclass prototype, the mixin function needs to be supplied by the user. An example of a mixin like function would be jQuery.extend(). function MyClass() {
SuperClass.call(this);
OtherSuperClass.call(this);
} MyClass.prototype = Object.create(SuperClass.prototype); // inherit
mixin(MyClass.prototype, OtherSuperClass.prototype); // mixin MyClass.prototype.myMethod = function() {
// do a thing
}; // Using propertiesObject argument with Object.create()
var o; // create an object with null as prototype
o = Object.create(null); o = {};
// is equivalent to:
o = Object.create(Object.prototype); // Example where we create an object with a couple of sample properties.
// (Note that the second parameter maps keys to *property descriptors*.)
o = Object.create(Object.prototype, {
// foo is a regular 'value property'
foo: { writable: true, configurable: true, value: 'hello' },
// bar is a getter-and-setter (accessor) property
bar: {
configurable: false,
get: function() { return 10; },
set: function(value) { console.log('Setting `o.bar` to', value); }
/* with ES5 Accessors our code can look like this
get function() { return 10; },
set function(value) { console.log('setting `o.bar` to', value); } */
}
}); function Constructor() {}
o = new Constructor();
// is equivalent to:
o = Object.create(Constructor.prototype);
// Of course, if there is actual initialization code in the
// Constructor function, the Object.create() cannot reflect it // Create a new object whose prototype is a new, empty object
// and add a single property 'p', with value 42.
o = Object.create({}, { p: { value: 42 } }); // by default properties ARE NOT writable, enumerable or configurable:
o.p = 24;
o.p;
// o.q = 12;
for (var prop in o) {
console.log(prop);
}
// 'q' delete o.p;
// false // to specify an ES3 property
o2 = Object.create({}, {
p: {
value: 42,
writable: true,
enumerable: true,
configurable: true
}
}); // Polyfill
// This polyfill covers the main use case which is creating a new object for which the prototype has been chosen but doesn't take the second argument into account. // Note that while the setting of null as [[Prototype]] is supported in the real ES5 Object.create, this polyfill cannot support it due to a limitation inherent in versions of ECMAScript lower than 5.
if (typeof Object.create != 'function') {
Object.create = (function() {
var Temp = function() {};
return function (prototype) {
if (arguments.length > 1) {
throw Error('Second argument not supported');
}
if(prototype !== Object(prototype) && prototype !== null) {
throw TypeError('Argument must be an object or null');
}
if (prototype === null) {
throw Error('null [[Prototype]] not supported');
}
Temp.prototype = prototype;
var result = new Temp();
Temp.prototype = null;
return result;
};
})();
}

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

  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. Eclipse编译问题

    问题现象:Maven编译ok,Eclipse始终存在编译错误,点了工程的刷新,没用,点了Eclipse上面的菜单Project -> Clean,也没用.后来看了下工作空间项目目录,发现.cla ...

  2. 使用Octave分析GNU Radio的数据

    Octave 是 GNU Radio 的最流行的分析工具,因此 GNU Radio 软件包也包含它自身的一组脚本用于读取和语法分析输出.本文介绍如何使用 Octave 分析 GNU Radio 产生的 ...

  3. 阿里云视频点播 php开发

    先购买开通阿里云的<视频点播>服务,视频点播 可以购买套餐 ,我在项目中使用的是299套餐 开发前在<用户信息管理>生成Access Key Secret,开发密钥使用 阿里云 ...

  4. (转)winform安装项目、安装包的制作、部署

    本文转载自:http://zhan.renren.com/cxymst?gid=3602888498037535727&from=post&checked=true 1,解决方案—添加 ...

  5. Timesten 日常管理命令合集

    Timesten 日常管理命令合集 以下所有操作都是基于TT  11 版,早前版本本人没用过,命令是否适用我不清楚啊! 各类服务管理 一.TT的启停  停服务:  1.停止复制与cache 进程:  ...

  6. 由 MySQL server 和 mysql-connector 版本的不匹配引发的一场惊魂

    剧情还原 今天原计划给领导演示一个小Demo, 昨天在自己机器上调通OK以后就下班了... 今天上午早会后,领导说 “昨天,我让我们IT同事把新的测试环境搭建好了,XXX 你把要演示的Demo部署到上 ...

  7. git 怎么上传文件到github上

    1.安装git     sudo  apt-get install git 2.配置全局变量     git config --global user.name langhunm     git co ...

  8. apt-get upgarde 和dist-upgrade的差别

    Debian/Ubuntu Linux都使用apt,升级时都是: apt-get update apt-get upgrade apt-get dist-upgrade 安装或升级系统分下面几个步骤. ...

  9. 关于Spring的Quartz的xml配置的例子

    <span style="font-size:16px"></span><h3><span style="font-family ...

  10. Cobalt strike 第二节生成报告

    0x00前言: 上一节我们说了怎么连接到服务器 0x01生成报告: 首先打开Cobalt Strike 点击Cobalt Strike -> Preferences Preferences Pe ...