�怼∈紫刃枰�说明的是,本文将直接讲解创建和使用类的各种技巧,一些基础的东西不再做解释,要理解如何在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(); // ?