Last active
August 29, 2015 14:01
-
-
Save rhgb/0f709c51f73eb0dc8fed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 思考题答案 */ | |
function createObject() { | |
var privateMember = 10; | |
var privateFunction = function(n){ | |
privateMember = n * 10; | |
} | |
return { | |
getMember: function() { | |
return privateMember; | |
} | |
setMember: function(num) { | |
privateFunction(num); | |
} | |
} | |
} | |
/* 引用类型 */ | |
var a = { | |
num: 1; | |
add: function(n) { | |
return n + this.num; | |
} | |
} | |
a.other = 2; | |
var b = a.add(5); | |
function add(n) { n = n + 1; } // ✗ | |
function add(obj) { obj.n = obj.n + 1; } // ✓ | |
/* 构造函数 */ | |
function Person() { | |
this.name = 'Nicholas'; | |
this.age = 20; | |
this.getName = function(){ | |
return this.name; | |
} | |
} | |
var p = new Person(); | |
p.getName(); // 'Nicholas' | |
/* 构造函数判断 */ | |
p.constructor === Person; // true | |
p instanceof Person; // true | |
p instanceof Object; // true | |
/* 关于构造函数 */ | |
Person(); // apply to global | |
var p = {}; | |
Person.apply(p); | |
function Person(name) { this.name = name; } | |
var p = new Person('Ben'); | |
p1 = new Person(); | |
p2 = new Person(); | |
p1.getName === p2.getName; // false | |
/* 原型模式 */ | |
function Person() { } | |
Person.prototype.name = 'Anne'; | |
Person.prototype.age = 20; | |
Person.prototype.getName = function() { | |
return this.name; | |
}; | |
var p = new Person(); | |
p.getName(); // 'Anne' | |
/* 原型对象 */ | |
Person.prototype.isPrototypeOf(p1); // true | |
Object.getPrototypeOf(p1); // Person.prototype | |
var p1 = new Person(); | |
p1.name = 'Dennis'; | |
for (var key in p1) { | |
alert(key); // name, value, getName | |
if (p1.hasOwnProperty(key)) { | |
alert(key); // name | |
} | |
} | |
/* 替代原型对象 */ | |
function Person() { } | |
Person.prototype = { | |
constructor: Person, | |
name: 'Charles', | |
age: 20, | |
getName: function() { return this.name; } | |
} | |
/* 原生对象的原型 */ | |
// some other js lib | |
Array.prototype.indexOf = function(){/*...*/} | |
// your js | |
var a = [1, 2, 3, 4]; | |
for (var i in a) { | |
console.log(a[i]); | |
// 1, 2, 3, 4, (function) | |
} | |
/* 原型模式的问题 */ | |
function SomeClass() { } | |
SomeClass.prototype.arr = []; | |
var s1 = new SomeClass(), | |
s2 = new SomeClass(); | |
s1.arr.push(123); | |
/* 组合使用构造函数和原型 */ | |
function Person() { | |
this.name = 'Jack'; | |
this.age = 20; | |
} | |
Person.prototype.getName = function() { | |
return this.name; | |
} | |
var p = new Person(); | |
/* 原型链继承 */ | |
function Super() { | |
this.prop = 123; | |
} | |
Super.prototype.getProp = function() { | |
return this.prop; | |
} | |
function Sub() { | |
this.subProp = 456; | |
} | |
Sub.prototype = new Super(); | |
Sub.prototype.getSubProp = function() { | |
return this.subProp; | |
} | |
var obj = new Sub(); | |
/* 继承关系判断 */ | |
obj instanceof Sub // true | |
obj instanceof Super // true | |
Sub.prototype.isPrototypeOf(obj) // true | |
Super.prototype.isPrototypeOf(obj) // true | |
/* 组合式继承 */ | |
function Super() { | |
this.prop = 123; | |
} | |
Super.prototype.getProp = function() { | |
return this.prop; | |
} | |
function Sub() { | |
Super.call(this); | |
this.subProp = 456; | |
} | |
Sub.prototype = new Super(); | |
Sub.prototype.getSubProp = function() { | |
return this.subProp; | |
} | |
/* Neuron OOP */ | |
var Sub = NR.Class({ | |
Extends: Super, | |
initialize: function(p) { | |
this.subProp = p; | |
} | |
getSubProp: function() { | |
return this.subProp; | |
}, | |
}); | |
function Class(prop) { | |
function newClass(){ | |
var init = initialize; | |
if(init){ | |
return init.apply(this, arguments); | |
} | |
} | |
var base = prop.Extends, | |
initialize = prop.initialize; | |
delete prop.Extends; | |
delete prop.initialize; | |
var F = function(){}; | |
F.prototype = base.prototype; | |
newProto = new F(); | |
NR.mix(newProto, prop); | |
newClass.prototype = newProto; | |
newProto.constructor = newClass; | |
return newClass; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment