我知道可以添加到函数的原型中,以便
function main(){}
main.prototype.load = function()
{
}
...并运行名为main.load的函数。
在原型中创建函数的原型是可能的吗?换句话说,我可以这样做吗:
main.prototype.get = function(){}
main.prototype.get.prototype.registration = function()
{
// load registration info
}并使用main.get.registration();调用该函数
当我尝试执行此操作时,控制台中显示以下错误消息:
Uncaught TypeError: Object function (){} has no method 'registration'
编辑:我是在调用new main();之后执行此操作的。所以我会做像这样的事情
var thisMain = new main();
thisMain.get.registration();发布于 2013-03-08 11:21:40
我认为你对原型有一点误解。
给定一个函数Foo,Foo.prototype不是Foo对象的原型。它是将分配给使用new Foo()创建的对象的原型。例如:
// This is a constructor that creates objects whose prototype is Person.prototype
var Person = function(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
console.log("Hello, my name is " + this.name);
}
var drew = new Person('Drew');
drew.sayHello(); // <-- Logs a message
drew.__proto__; // <-- Not part of the Javascript spec, but it some browsers this is a reference to Person.prototype您的main.get.registration可以在没有原型的情况下实现:
main = function() {/* do stuff*/}
main.get = function() {/* this is a getter function? */}
main.get.registration = function() {/* I don't know what this does */}您希望创建什么样的接口或API?是否涉及使用new创建对象
更新:是许多实现你想要的东西的可能方法之一:
main = function() {
// store a reference to this instance.
var self = this;
// Construct the get object. It doesn't need to be a function because it's never invoked
this.get = {};
this.get.registration = function() {
// Use self to refer to the specific instance of main you're interacting with.
retrieveRegistrationFor(self); // <-- pseudo-code
}
}更新2:这里介绍了如何使用构造函数构造get对象,从而允许您对所有内容使用原型。我已经将构造函数的名称大写,这是有助于区分普通函数/方法和构造函数的最佳实践。
// Constructor for the get object. This is only ever invoked in Main's constructor.
Getter = function(mainInstance) {
this.self = mainInstance;
}
Getter.prototype.registration = function() {
retrieveRegistrationFor(this.self); // <-- pseudo-code
}
Main = function() {
// Construct the get object and attach it to this object.
this.get = new Getter(this);
}正如其他答案所指出的,在Javascript中有很多构造对象的方法。这完全取决于情况和您个人的编码风格。
发布于 2013-03-08 11:21:17
我把它用上了
main.prototype.get.prototype.registration();但是请记住,正如@the_system提到的,您不能直接使用main.get;您必须通过原型来查找get函数(以及与registration函数的相似性)。
发布于 2013-03-08 11:37:15
这只是我个人的观点,但我一直发现JavaScript中的原型继承模型很难理解。在编写代码时很难推理,而在6个月后维护代码则更难推理。
然而,我认为您真正想要问的是:“我能编写一个继承匿名类成员方法的类吗?”当你以这种方式重新表述时,我认为很明显,这种方法有不确定的价值。编写类的整个目的是支持简单的抽象和封装,同时保持组合紧凑。
使用传统型对象ala会更简单:
var main = {
get: {
registration: function() {
//TODO
}
}
}main.get.registration()就像馅饼一样简单。如果可以利用Object.create()和Object.defineProperties()来实现这一点,那就更好了。
如果你一定要使用原型继承,我喜欢simple Function.prototype extension that Mr. Kistner proposes:
Function.prototype.inheritsFrom = function(parentClassOrObject) {
if (parentClassOrObject.constructor === Function) {
//Normal Inheritance
this.prototype = new parentClassOrObject;
this.prototype.constructor = this;
this.prototype.parent = parentClassOrObject.prototype;
} else {
//Pure Virtual Inheritance
this.prototype = parentClassOrObject;
this.prototype.constructor = this;
this.prototype.parent = parentClassOrObject;
}
return this;
};这使您可以像这样编写类和继承:
/***
* Method to create a Class with optional inheritance.
* Generally, I oppose this semantic in JS:
* partly because of the ineffability of the 'this' operator,
* and partly because of the difficulty in grokking this.
* What we're really saying here (through the wonders of functional programming) is this:
*
* var MyClass1 = function(param1) {
* var ret = this;
* ret.id = param1;
* return ret;
* };
*
* var MyClass2 = function(param1, param2) {
* var ret = this;
* MyClass1.apply(this, Array.prototype.slice.call(arguments, 0));
* ret.name = param2;
* return ret;
* };
*
* MyClass2.prototype = new MyClass1;
* MyClass2.prototype.constructor = MyClass1;
* MyClass2.prototype.parent = MyClass1.prototype;
*
* I find this whole mode of operation as dull as it is stupid.
* Nonetheless, there are occasions when the convention is suitable for type/instance checking
*
* Obviously, this method has very little utility if you are not using prototypal inheritance
*/
var MyClassCreatorMethod = function(name, inheritsFrom, callBack) {
var obj = Object.create(null);
obj[name] = function() {
try {
if(inheritsFrom ) {
inheritsFrom.apply(this, Array.prototype.slice.call(arguments, 0));
}
callBack.apply(this, Array.prototype.slice.call(arguments, 0));
} catch(e) {
//do something
}
};
if(inheritsFrom) {
obj[name].inheritsFrom(inheritsFrom);
}
return obj[name];
};至此,菊花链继承类就变得微不足道了。我只是从我的一个项目中引出了这一点,所以并不是所有的语义都适用于您--它只是为了说明一种以一种更容易理解的方式将行为功能化的方法。
https://stackoverflow.com/questions/15286053
复制相似问题