面向对象的JavaScript-004
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的更多相关文章
- 前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型
前端开发:面向对象与javascript中的面向对象实现(二)构造函数与原型 前言(题外话): 有人说拖延症是一个绝症,哎呀治不好了.先不说这是一个每个人都多多少少会有的,也不管它究竟对生活有多么大的 ...
- 前端开发:面向对象与javascript中的面向对象实现(一)
前端开发:面向对象与javascript中的面向对象实现(一) 前言: 人生在世,这找不到对象是万万不行的.咱们生活中,找不到对象要挨骂,代码里也一样.朋友问我说:“嘿,在干嘛呢......”,我:“ ...
- 面向对象的 JavaScript
面向对象的javascript 一.创建对象 创建对象的几种方式: var obj = {}; var obj = new Object(); var obj = Object.create(fath ...
- 摘抄--全面理解面向对象的 JavaScript
全面理解面向对象的 JavaScript JavaScript 函数式脚本语言特性以及其看似随意的编写风格,导致长期以来人们对这一门语言的误解,即认为 JavaScript 不是一门面向对象的语言,或 ...
- 面向对象的JavaScript --- 动态类型语言
面向对象的JavaScript --- 动态类型语言 动态类型语言与面向接口编程 JavaScript 没有提供传统面向对象语言中的类式继承,而是通过原型委托的方式来实现对象与对象之间的继承. Jav ...
- 面向对象的JavaScript --- 封装
面向对象的JavaScript --- 封装 封装 封装的目的是将信息隐藏.一般而言,我们讨论的封装是封装数据和封装实现.真正的封装为更广义的封装,不仅包括封装数据和封装实现,还包括封装类型和封装变化 ...
- 面向对象的JavaScript --- 多态
面向对象的JavaScript --- 多态 多态 "多态"一词源于希腊文 polymorphism,拆开来看是poly(复数)+ morph(形态)+ism,从字面上我们可以理解 ...
- 面向对象的JavaScript --- 原型模式和基于原型继承的JavaScript对象系统
面向对象的JavaScript --- 原型模式和基于原型继承的JavaScript对象系统 原型模式和基于原型继承的JavaScript对象系统 在 Brendan Eich 为 JavaScrip ...
- 第1章 面向对象的JavaScript
针对基础知识的每一个小点,我都写了一些小例子,https://github.com/huyanluanyu1989/DesignPatterns.git,便于大家理解,如有疑问,大家可留言给我,最近工 ...
- javascript面向对象之Javascript 继承
转自原文javascript面向对象之Javascript 继承 在JavaScript中实现继承可以有多种方法,下面说两种常见的. 一,call 继承 先定义一个“人”类 //人类 Person=f ...
随机推荐
- protobuf新增message报错:类型已存在
问题现象:在一个已有的proto文件(RecommendResponse.proto)中新增一个message(BookList),用maven编译proto文件时报错: E:\workspace\m ...
- C# datatable竖行转换的问题
这次在做项目中,遇到了这样一个问题:datable中列头的名字是不确定的,从数据库中动态查出来的,假设为typeDATA,行的数据中又包含了列头的信息,并按固定的字段分组,当查处行的数据之后用来填充每 ...
- Synergy CORTEX M 启动流程
1.启动文件“startup_S7G2.c” 中断向量表地址指针:“0xe000ed08” /* Vector table. */ BSP_DONT_REMOVE const exc_ptr_t __ ...
- 雅虎CSS初始化代码
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,b ...
- 网络异常时抓包操作说明tcpdump+Wireshark
转债至 https://help.aliyun.com/knowledge_detail/40564.html?spm=5176.11065259.1996646101.searchclickresu ...
- 7503E-M-irf2配置以及bfd配置
IRF2配置 irf domain 10 irf mac-address persistent always irf auto-update enable irf auto-merge enable ...
- 编译hostapd时,出现错误:/usr/bin/ld: cannot find -lnl
book@ubuntu:/work/project/wifi/04.hostapd/hostapd-2.0/hostapd$ make /usr/bin/ld: cannot find -lnl co ...
- ORACLE删除字段(set unused的用法)
一.问题 现场有一张大数据量的分区表,数据量在10G以上.因某种原因需要删除其中的某些字段.如果直接用 alter table1 drop (column1,column2); 或者alter tab ...
- java中Thursday 05 September 2002类型时间的转化
package config; import Java.text.DateFormat; import java.text.ParseException; import java.text.Simpl ...
- Spring中引质增强的安全
在引质增强中使用ThreadLocal变量,是因为控制状态使代理类变成了非线程安全的实例,为了解决单线程安全的问题,通过ThreadLocal让每个线程单独使用一个状态.