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. 查看Google Cloud的IP地址段

    for LINE in `dig txt _cloud-netblocks.googleusercontent.com +short | tr " " "\n" ...

  2. JDK 8 新特性

    JDK 8, Oracle's implementation of Java SE 8. JDK 8 是 Oracle 对 Java SE 8 规范的实现. 本文分析 JDK 8 引入的新特性. 官方 ...

  3. Hibernate学习5—Hibernate操作对象2

    第二节:Session 常用方法讲解 2)load和get()方法: 数据库中不存在与OID 对应的记录,Load()方法会抛出异常: load方法默认采用延迟加载,load出来的对象是一个代理类.开 ...

  4. iframe显示跨域url页面

    可以通过JS搞定跨域问题,但是比较麻烦,我就在后台处理了,其实这样本质上也算不上是跨域了 后台otherwebsite.php: <?php $url="www.otherwebsit ...

  5. springmvc 打包遇到的问题

    1.测试有错误,过不去,mvn install -DSkipTests 2.设置resources路径,在pom.xml中添加 <build> <resources> < ...

  6. CentOS 7 需要安装的常用工具,及centos安装fcitx 搜狗输入法的坑旅

    https://blog.csdn.net/tham_/article/details/41868831 Centos常用设置 1.当最大化时隐藏标题栏 或者使用tweak tool 在字体中将标题栏 ...

  7. scp 远程复制

    1. 上传本地文件到远程机器指定目录 命令: scp /opt/soft/nginx-0.5.38.tar.gz root@192.168.120.204:/opt/soft/scptest 上传本地 ...

  8. 脱壳系列(五) - MEW 壳

    先用 PEiD 看一下 MEW 11 1.2 的壳 用 OD 载入程序 按 F8 进行跳转 往下拉 找到这个 retn 指令,并下断点 然后 F9 运行 停在该断点处后再按 F8 右键 -> 分 ...

  9. java并发的基本概念和级别

    并发的概念: 并发(Concurrency)和并行(Parallelism) 并发偏重于多个任务交替执行,而多个任务之间有可能还是串行的.而并行是真正意义上的“同时执行”.严格意义上来说,并行的多个任 ...

  10. 关于git的reset、checkout、revert

    https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting/file-level-operations 最 ...