如何创建构造函数在javascript中创建构造函数?
有时候,在JavaScript中,我需要许多构造函数和对象来创建伪类,因为我非常喜欢objective,所以我会执行以下操作:如何创建构造函数在javascript中创建构造函数?,javascript,constructor,Javascript,Constructor,有时候,在JavaScript中,我需要许多构造函数和对象来创建伪类,因为我非常喜欢objective,所以我会执行以下操作: var anyClass = (function (settings) { var staticPrivate = {}, staticPublic = function () { var public = this, private = {}; (function constructor (here, the, args) {
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
}
}
});
但我的问题是,我不知道如何使用构造函数类创建原型/构造函数。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
我希望你能帮助我。谢谢…我理解我的错误,现在当我有一个构造函数时,我可以返回一些东西
var Class=function(){}代码>和带有的闭包函数中的类.prototype
。apply(这是参数)
完成了这件事,这就是为什么我可以在构造函数中返回类
,如果我愿意的话
var Class = function () {
var ClassICreate = function () {};
...
return ClassICreat;
}
它不起作用,因为您不能从构造函数返回,因为它是一个对象
这就是我的工作原理:
var Class = (function () {
var Class = function () {
return (function (information) {
var Class = function () {};
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(Class.prototype, properties);
return Class;
}).apply(this, arguments);
};
return Class;
})();
在我找到答案后,我觉得很容易,感谢他们的评论,他们帮助我找到了正确的答案…Javascript不是面向对象的语言。每次我使用(或滥用)Javascript作为面向对象的语言,我都会陷入一片混乱。@maze-le-see@maze-le:Javascript是一种面向对象的语言。这不是一种基于类的语言。我想你已经读过了: