MooTools Class 使用、继承详解
? 首先需要说明的是,本文将直接讲解创建和使用类的各种技巧,一些基础的东西不再做解释,要理解如何在JavaScript中实现面向对象的设计,请先参考《JavaScript.高级程序设计(第2版)》(前7章)、《javascript.设计模式》(前四章)、《JavaScript.语言精粹》这三部经典之作。
- var Person = new Class({
- // Methods
- initialize: function (name, age) {
- this.name = name;
- this.age = age;
- },
- log: function () {
- console.log(this.name + ',' + this.age);
- }
- });
- var mark = new Person('mark', 24);
- mark.log(); // returns 'mark,24'
- var Person = new Class(function (name, age) {
- this.name = name;
- this.age = age;
- });
- Person.implement('log', function () {
- console.log(this.name + ',' + this.age);
- });
- var mark = new Person('mark', 24);
- mark.log(); // returns 'mark,24'
- Person.implement('log', function () {
- console.log(this.name + ',' + this.age);
- });
- Person.implement('city', '深圳');
- Person.implement({
- 'city': '深圳',
- 'log': function () {
- console.log(this.name + ',' + this.age);
- }
- });
- var Person = new Class(function (name, age) {
- this.name = name;
- this.age = age;
- });
- Person.implement({
- instanceMethod: function () {
- console.log('From an instance!');
- }
- });
- Person.extend({
- classMethod: function () {
- console.log('From the class itself!');
- }
- });
- var mark = new Person('mark', 24);
- console.log(typeOf(mark.instanceMethod)); // returns 'function'
- mark.instanceMethod(); // returns 'From an instance!'
- console.log(typeOf(mark.classMethod)); // returns 'null',说明实例是不能调用静态方法的
- console.log(typeOf(Person.classMethod)); // returns 'function'
- Person.classMethod(); // returns 'From the class itself!'
- console.log(typeOf(Person.instanceMethod)); // returns 'null',同样类也不能直接调用为实例而创建的方法
- Person.prototype.instanceMethod(); // 类只能通过这种方式调用原型上的方法
- var Person = (function () {
- // 私有变量
- var numOfPersons = 0;
- // 私有方法
- var formatName = function (name) {
- return name.capitalize();
- };
- return new Class({
- initialize: function (name, age) {
- this.name = name;
- this.age = age;
- numOfPersons++;
- },
- // 公有方法
- log: function () {
- console.log(formatName(this.name) + ',' + this.age);
- },
- getNumOfPersons: function () {
- return numOfPersons;
- }
- });
- })();
- var mark = new Person('mark', 24);
- mark.log(); // returns 'mark,24'
- console.log(mark.getNumOfPersons()); // returns 1
- var Person = (function () {
- // 私有变量
- var name = '';
- return new Class({
- initialize: function (v1, v2) {
- name = v1;
- this.age = v2;
- },
- getName: function () {
- return name;
- },
- setName: function (value) {
- name = value;
- },
- getAge: function () {
- return this.age;
- },
- setAge: function (value) {
- this.age = value;
- }
- });
- })();
- var mark = new Person('mark', 24);
- console.log(mark.getName()); // 'mark'
- mark.setName('grey');
- console.log(mark.getName()); // 'grey'
- console.log(mark.getAge()); // 24
- var john = new Person('john', 18);
- console.log(john.getName()); // 'john'
- console.log(john.getAge()); // 18
- console.log(mark.getName()); // 'john'
- console.log(mark.getAge()); // 24
- var Person = (function () {
- // 私有变量
- var AGE_UPPER_BOUND = 32;
- return new Class({
- initialize: function (v1, v2) {
- // ...
- },
- getAGEUPPERBOUND: function (value) {
- return AGE_UPPER_BOUND;
- }
- });
- })();
- var Person = (function () {
- // 私有变量
- var constants = {
- AGE_UPPER_BOUND: 32,
- AGE_LOWER_BOUND: 18
- };
- return new Class({
- initialize: function (v1, v2) {
- // ...
- },
- getConstants: function (name) {
- return constants[name];
- }
- });
- })();
- var Animal = new Class({
- initialize: function (age) {
- this.age = age;
- }
- });
- var Cat = new Class({
- Extends: Animal,
- initialize: function (name, age) {
- this.parent(age); // calls initalize method of Animal class
- this.name = name;
- }
- });
- var cat = new Cat('Micia', 20);
- console.log(cat.name); // 'Micia'
- console.log(cat.age); // 20
- var Mixin = new Class({
- getName: function () {
- return this.name;
- },
- setName: function (value) {
- this.name = value
- }
- });
- var Cat = new Class({
- Extends: Animal,
- Implements: Mixin,
- initialize: function (name, age) {
- this.parent(age); // calls initalize method of Animal class
- this.name = name;
- }
- });
- var cat = new Cat('Micia', 20);
- console.log(cat.name); // 'Micia'
- console.log(cat.age); // 20
- cat.setName('Dog');
- console.log(cat.getName()); // 'Dog'
- // mixin对象存储一些通用方法,可以被不同的类implement
- var objMixin = (function () {
- var counter = 0;
- return {
- init: function () {
- counter += 1;
- },
- getCounter: function () {
- return counter;
- },
- getAge: function () {
- return this.age;
- },
- setAge: function (value) {
- this.age = value;
- }
- };
- })();
- var Cat = new Class({
- Extends: Animal,
- Implements: Mixin,
- initialize: function (name, age) {
- this.parent(age); // calls initalize method of Animal class
- this.name = name;
- }
- });
- Cat.implement(objMixin);
- var Dog = new Class({
- Extends: Animal,
- Implements: Mixin,
- initialize: function (name, age) {
- this.parent(age); // calls initalize method of Animal class
- this.name = name;
- }
- });
- Dog.implement(objMixin);
- var cat = new Cat('Micia', 20);
- console.log(cat.name); // 'Micia'
- console.log(cat.age); // 20
- cat.setName('汤姆');
- console.log(cat.getName()); // '汤姆'
- cat.setAge(12);
- console.log(cat.getAge()); // 12
- // 对mixin对象的私有属性进行操作
- cat.init();
- console.log(cat.getCounter()); // 1
- var dog = new Dog('小狗', 6);
- console.log(dog.name); // '小狗'
- console.log(dog.age); // 6
- dog.setName('布鲁托');
- console.log(dog.getName()); // '布鲁托'
- dog.setAge(8);
- console.log(cat.getAge()); // 8
- // 对mixin对象的私有属性进行操作
- dog.init();
- console.log(dog.getCounter()); // 2
- console.log(cat.getCounter()); // 2
- var obj = {
- Waa: "Waa",
- aa: 'aa',
- 68: '68',
- 15: '15',
- tt: 'tt',
- '-7': '-7',
- _: "___",
- online: true
- };
- for (var k in obj) {
- console.log(k);
- }
- var Super = new Class({
- log: function () {
- console.log('Super');
- }
- });
- var Mixin = new Class({
- log: function () {
- console.log('Mixin');
- }
- });
- var Sub = new Class({
- Extends: Super,
- Implements: Mixin
- });
- var obj = new Sub();
- obj.log(); // ?
- var Sub = new Class({
- Implements: Mixin,
- Extends: Super
- });
- var obj = new Sub();
- obj.log(); // ?
- var Sub = new Class({
- Implements: Mixin,
- Extends: Super,
- log: function () {
- console.log('sub');
- }
- });
- var obj = new Sub();
- obj.log(); // ?
- var objMixin = {
- log: function () {
- console.log('objMixin');
- }
- };
- var Sub = new Class({
- Implements: Mixin,
- Extends: Super,
- log: function () {
- console.log('sub');
- }
- });
- Sub.implement(objMixin);
- var obj = new Sub();
- obj.log(); // ?
转载于:https://blog.51cto.com/hmking/682098
MooTools Class 使用、继承详解相关推荐
- python类继承中构造方法_第8.3节 Python类的__init__方法深入剖析:构造方法与继承详解...
第8.3节Python类的__init__方法深入剖析:构造方法与继承详解 一. 引言 上两节介绍了构造方法的语法及参数,说明了构造方法是Python的类创建实例后首先执行的方法,并说明如果类没 ...
- (117)System Verilog类继承详解
(117)System Verilog类继承详解 1.1 目录 1)目录 2)FPGA简介 3)System Verilog简介 4)System Verilog类继承详解 5)结语 1.2 FPGA ...
- 【职坐标】java面向对象三大特性(二)——继承详解
[职坐标]java面向对象三大特性(二)--继承详解 a) 什么是继承? i. 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可 b) ...
- C++继承详解之四——is-a接口继承和has-a实现继承
在学习继承的过程中,不管是在书中还是在网上找资料,都跟多态分不开,其中还有个很抓人眼球的问题,那就是书上总是说的is-a关系和has-a关系. 很多书中讲到继承时都会说: public继承是一个接口继 ...
- JavaScript继承详解(四)
文章截图 - 更好的排版 在本章中,我们将分析Douglas Crockford关于JavaScript继承的一个实现 - Classical Inheritance in JavaScript. C ...
- JavaScript继承详解(四) 转
在本章中,我们将分析Douglas Crockford关于JavaScript继承的一个实现 - Classical Inheritance in JavaScript. Crockford是Java ...
- python3类的继承详解_python3中类的继承以及self和super的区别详解
python中类的继承: 子类继承父类,及子类拥有了父类的 属性 和 方法. python中类的初始化都是__init__().所以父类和子类的初始化方式都是__init__(),但是如果子类初始化时 ...
- C++继承和多态特性——继承详解(2)
目录 一.派生类和基类的构造析构关系 1.派生类并不继承基类的构造和析构函数,只继承成员变量和普通成员方法 2.派生类的构造函数一定会调用基类的构造函数,析构也一样 3.为什么派生类的构造(析构)必须 ...
- 组合、聚合、继承详解
有人学了继承,认为他是面向对象特点之一,就在所有能用到继承的地方使用继承,而不考虑究竟该不该使用,无疑,这是错误的.那么,究竟该如何使用继承呢? java中类与类之间的关系 大部分的初学者只知道jav ...
最新文章
- SAP MM 对于MRKO事务代码的几点优化建议
- c++ explicit(显式)关键字
- python interpreter 中没有torch_PyTorch扩展自定义PyThon/C++(CUDA)算子的若干方法总结
- JQuery中的.attr()与.removeAttr()
- rapidjson 嵌套json_[整理][RapidJson用法2]C++之RapidJson创建嵌套数组json | 勤奋的小青蛙...
- mfc大观之五、六(消息机制和消息运行)
- Windows 编程[20] - 改变菜单项并换行
- 二十四种设计模式:策略模式(Strategy Pattern)
- 二分法猜数——C/C++
- MySQL_DBA整理
- 500个运营工具大全,速度收藏!!!
- 前端 后端 MD5加盐
- 【Redis系列】Redis 入门指南
- hibernate 注解方式 属性mappedBy详解
- 设置华表Cell插件外观时的“闪烁”问题
- python 计算字符串表达式_python计算数学表达式
- nmn抗衰老是骗局吗,美国nmn骗局,正面解答
- 关于我很想吐槽公司的策划同学这件事
- Java异常 | JedisException: Could not get a resource from the pool
- android 5.1 随机IMEI号