3
有時候在JavaScript中,我需要僞類許多構造函數和對象,因爲我喜歡的目標很讓我做這樣的事情:如何創建一個構造函數在javascript中創建構造函數?
var anyClass = (function (settings) {
var staticPrivate = {}, staticPublic = function() {
var public = this, private = {};
(function constructor (here, the, args) {
this.hta = [here, the, args, arguments];
}).apply(this, arguments);
arguments = undefined;delete arguments;
private.stuff = function() {}
Object.defineProperties(public, {
"e.g. length": {
get: function() {
return private.length;
},
set: function (newValue) {
return;
},
enumerable: false
}
});
};
Object.defineProperties(staticPublic, {
"staticFinalHiddenString": {
get: function() {
return "YEAH, I'm static and final and hidden...";
},
set: function (newValue) {
return "You cannot set me.. :P";
},
enumerable: false
}
});
staticPrivate.some = function (init) {
if (settings.some == "settings") init();
}
window.requestAnimationFrame(function() {
staticPrivate.some(function (I) {
run(on, first, render);
});
});
return staticPublic;
})({
some: "settings",
here: null
});
而且,每一次,所以現在我想創建一個新的類的構造函數爲了我。我覺得這個:
new Class({
constructor: function (here) {
is(my + constructor);
},
properties: {
name: {
getter: function() {},
setter: function (newValue) {},
hidden: false,
static: false,
final: false
},
version: {
getter: function() {
return 0.3;
},
setter: function (newValue) {},
hidden: true,
static: true,
final: true
}
}
});
但我的問題是,我不知道如何創建具有構造class.prototype.prototype不起作用的原型/構造。
我只是去嘗試:
var Class = (function() {
var Class = (function() {
var constructor = function() {
return (function (information) {
this.prototype = {};
var properties = {};
for(var key in information.properties) {
properties[key] = {
get: information.properties[key].getter,
set: information.properties[key].setter,
enumerable: !information.properties[key].hidden || true
};
};
Object.defineProperties(this.prototype, properties);
return this;
}).apply(this, arguments);
};
return constructor;
})();
return Class;
})();
這並不爲我工作:C
我希望你能幫助我。謝謝...
Javascript不是面向對象的語言。每次我使用(或濫用)JavaScript作爲面向對象的語言,我都陷入了一片混亂。 –
@ maze-le參見[面向對象的JavaScript簡介](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript) – Aprillion
@ maze-le:Javascript是一個面嚮對象語言。這不僅僅是一種基於班級的語言。 – slebetman