博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面向对象的JavaScript-004
阅读量:5282 次
发布时间:2019-06-14

本文共 4831 字,大约阅读时间需要 16 分钟。

1.

 

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.  2     // Shape - superclass  3     function Shape() {  4       this.x = 0;  5       this.y = 0;  6     }  7   8     // superclass method  9     Shape.prototype.move = function(x, y) { 10       this.x += x; 11       this.y += y; 12       console.info('Shape moved.'); 13     }; 14  15     // Rectangle - subclass 16     function Rectangle() { 17       Shape.call(this); // call super constructor. 18     } 19  20     // subclass extends superclass 21     Rectangle.prototype = Object.create(Shape.prototype); 22     Rectangle.prototype.constructor = Rectangle; 23  24     var rect = new Rectangle(); 25  26     console.log('Is rect an instance of Rectangle?', rect instanceof Rectangle);// true 27     console.log('Is rect an instance of Shape?', rect instanceof Shape);// true 28     rect.move(1, 1); // Outputs, 'Shape moved.' 29  30     // If you wish to inherit from multiple objects, then mixins are a possibility. 31     // 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(). 32      33     function MyClass() { 34       SuperClass.call(this); 35       OtherSuperClass.call(this); 36     } 37  38     MyClass.prototype = Object.create(SuperClass.prototype); // inherit 39     mixin(MyClass.prototype, OtherSuperClass.prototype); // mixin 40  41     MyClass.prototype.myMethod = function() { 42       // do a thing 43     }; 44      45  46     // Using propertiesObject argument with Object.create() 47     var o; 48  49     // create an object with null as prototype 50     o = Object.create(null); 51  52  53     o = {}; 54     // is equivalent to: 55     o = Object.create(Object.prototype); 56  57  58     // Example where we create an object with a couple of sample properties. 59     // (Note that the second parameter maps keys to *property descriptors*.) 60     o = Object.create(Object.prototype, { 61       // foo is a regular 'value property' 62       foo: { writable: true, configurable: true, value: 'hello' }, 63       // bar is a getter-and-setter (accessor) property 64       bar: { 65         configurable: false, 66         get: function() { return 10; }, 67         set: function(value) { console.log('Setting `o.bar` to', value); } 68     /* with ES5 Accessors our code can look like this 69         get function() { return 10; }, 70         set function(value) { console.log('setting `o.bar` to', value); } */ 71       } 72     }); 73  74  75     function Constructor() {} 76     o = new Constructor(); 77     // is equivalent to: 78     o = Object.create(Constructor.prototype); 79     // Of course, if there is actual initialization code in the 80     // Constructor function, the Object.create() cannot reflect it 81  82  83     // Create a new object whose prototype is a new, empty object 84     // and add a single property 'p', with value 42. 85     o = Object.create({}, { p: { value: 42 } }); 86  87     // by default properties ARE NOT writable, enumerable or configurable: 88     o.p = 24; 89     o.p; 90     // 42 91  92     o.q = 12; 93     for (var prop in o) { 94       console.log(prop); 95     } 96     // 'q' 97  98     delete o.p; 99     // false100 101     // to specify an ES3 property102     o2 = Object.create({}, {103       p: {104         value: 42,105         writable: true,106         enumerable: true,107         configurable: true108       }109     });110 111     // Polyfill112     // 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.113 114     // 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.115     if (typeof Object.create != 'function') {116       Object.create = (function() {117         var Temp = function() {};118         return function (prototype) {119           if (arguments.length > 1) {120             throw Error('Second argument not supported');121           }122           if(prototype !== Object(prototype) && prototype !== null) {123             throw TypeError('Argument must be an object or null');124          }125          if (prototype === null) { 126             throw Error('null [[Prototype]] not supported');127           }128           Temp.prototype = prototype;129           var result = new Temp();130           Temp.prototype = null;131           return result;132         };133       })();134     }

 

 

 

转载于:https://www.cnblogs.com/shamgod/p/5523387.html

你可能感兴趣的文章
Quartz.Net和Windows服务实现定时任务功能
查看>>
链表相加
查看>>
一块红布
查看>>
【java】之类加载机制
查看>>
java 实现图表展示
查看>>
前端基础之css
查看>>
javascript 事件
查看>>
x509数字证书导入-然后删除自身
查看>>
文件上传缺失common-io.jar包
查看>>
Extract Method
查看>>
自动化测试框架学习之一 --- 为什么要进行自动化测试?
查看>>
QT Creator 快速入门教程 读书笔记(二)
查看>>
百度云?极光?个推?
查看>>
vue2.0模拟锚点实现定位平滑滚动
查看>>
oracle 11g 命令 导入 导出表
查看>>
hdu 2766 Equilibrium Mobile
查看>>
2019春总结作业
查看>>
在Chrome Console中加载jQuery
查看>>
浅谈python 手机crash和app crash循环执行问题
查看>>
jQuery左侧菜单实例
查看>>