這是道格拉斯Crockford的原始來源,因爲它出現在他的幻燈片:
function constructor(spec) {
let {member} = spec,
{other} = other_constructor(spec),
method = function() {
// member, other, method, spec
};
return Object.freeze({
method,
other
});
}
下面的例子是一個更具體版本的道格拉斯克羅克福德的對象創作模式2014.
道格拉斯克羅克福德大量使用ECMAScript 6功能,如解構等。
與下列選項中啓動的Node.js代碼(啓用ES6):
node --harmony --harmony_destructuring demo.js
demo.js
// Douglas Crockford 2014 Object Creation
(function() {
'use strict';
function adress(spec) {
let {
street, city
} = spec,
logAdress = function() {
console.log('Adress:', street, city);
};
return Object.freeze({
logAdress
});
};
function person(spec) {
let {
preName,
name
} = spec, {
logAdress
} = adress(spec),
logPerson = function() {
// member, other, method, spec
console.log('Name: ', preName, name);
logAdress();
};
return Object.freeze({
logPerson,
logAdress
});
};
let myPerson = person({
preName: 'Mike',
name: 'Douglas',
street: 'Newstreet',
city: 'London'
});
myPerson.logPerson();
})();
據道格拉斯Crockford的談話,他避免了使用:
觀看原始克羅克福德視頻: https://www.youtube.com/watch?v=PSGEjv3Tqo0
的克羅克福德道格拉斯對象創建模式2014年很好的解釋是這樣的博客:https://weblogs.asp.net/bleroy/crockford%E2%80%99s-2014-object-creation-pattern
謝謝,這個對我有意義。類似於面嚮對象語言的類構造函數 – 2016-03-15 23:30:00