Javascript中原型函数的原型

Javascript中原型函数的原型,javascript,prototype,Javascript,Prototype,我知道可以在函数的原型中添加 function main(){} main.prototype.load = function() { } ... 然后运行名为main.load的函数 是否有可能在原型中创建一个函数的原型?换句话说,我可以这样做吗: main.prototype.get = function(){} main.prototype.get.prototype.registration = function() { // load registration info }

我知道可以在函数的原型中添加

function main(){}
main.prototype.load = function()
{

}
...
然后运行名为
main.load的函数

是否有可能在原型中创建一个函数的原型?换句话说,我可以这样做吗:

main.prototype.get = function(){}
main.prototype.get.prototype.registration = function()
{
    // load registration info
}
并使用
main.get.registration()调用函数

当我尝试执行此操作时,控制台中会显示以下错误消息:

uncaughttypeerror:对象函数(){}没有方法“注册”

编辑:我在调用
newmain()后执行此操作。所以我会做一些类似的事情

var thisMain = new main();
thisMain.get.registration();

我确实让它工作了

main.prototype.get.prototype.registration();

但是请记住,正如@the_系统所提到的,您不能直接使用
main.get
;您必须通过原型来找到
get
函数(以及与
注册
函数的相似性)。

我确实使用了它

main.prototype.get.prototype.registration();

但是请记住,正如@the_系统所提到的,您不能直接使用
main.get
;您必须通过原型查找
get
函数(以及与
注册
函数的相似性)。

我认为您对原型有点误解

给定函数
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
您希望创建什么样的接口或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
    }
}

正如其他答案所指出的,有很多方法可以在Javascript中构造对象。这完全取决于情况和您个人的编码风格。

我想您对原型有点误解了

给定函数
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
您希望创建什么样的接口或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
    }
}

正如其他答案所指出的,有很多方法可以在Javascript中构造对象。这完全取决于具体情况和您的个人编码风格。

这只是我个人的观点,但我总是发现JavaScript中的典型继承模型很难探索。编写代码时很难推理,6个月后维护代码时更难推理

然而,我想你真正要问的是:“我能写一个从匿名类继承其成员方法的类吗?”当你用这种方式重新表述它时,我想很明显,这种方法有不确定的价值。编写类的全部目的是支持简单的抽象和封装,同时保持紧凑的组合

使用传统对象ala会更简单:

var main = { 
    get: {
        registration: function() {
            //TODO
        }  
    }
}
main.get.registration()
非常简单。如果可以利用Object.create()和Object.defineProperties()来实现这一点,那就更好了

如果您必须使用原型继承,我喜欢:

这样,您就可以像这样组合类和继承:

/***
 * 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];
};

从这里开始,菊花链继承类就变得微不足道了。我只是从我的一个项目中提取了这个,所以并不是所有的语义都适用于你——这只是为了说明一种更容易推理的方式来实现行为功能化的方法。

这只是我个人的观点,但我总是发现JavaScript中的原型继承模型很难探索。编写代码时很难推理,6个月后维护代码时更难推理

然而,我想你真正要问的是:“我能写一个从匿名类继承其成员方法的类吗?”当你用这种方式重新表述它时,我想很明显,这种方法有不确定的价值。编写类的全部目的是支持简单的抽象和封装,同时保持紧凑的组合

使用传统对象ala会更简单:

var main = { 
    get: {
        registration: function() {
            //TODO
        }  
    }
}
main.get.registration()
非常简单。如果可以利用Object.create()和Object.defineProperties()来实现这一点,那就更好了

如果您必须使用原型继承,我喜欢:

这样,您就可以像这样组合类和继承:

/***
 * 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];
};

从这里开始,菊花链继承类就变得微不足道了。我刚从我的一个项目中提取了这一点,所以并不是所有的语义都适用于你——这只是为了说明一种更容易推理的功能化行为的方法。

也许你想做的是:

function main(){}
main.prototype.load = function()
{

};

main.prototype.get = function(){};
main.prototype.get.prototype.registration = function()
{
    // load registration info
    alert('hi, I\'m working');
};

var thisMain = new main();
var other = new thisMain.get();
other.registration();

也许你想做的是:

function main(){}
main.prototype.load = function()
{

};

main.prototype.get = function(){};
main.prototype.get.prototype.registration = function()
{
    // load registration info
    alert('hi, I\'m working');
};

var thisMain = new main();
var other = new thisMain.get();
other.registration();

这应该是可能的,但这似乎不是一个好主意。如果
这个
在JS中已经与一个原型混淆,想象一下有两个原型……首先,您无法直接从
main
访问
load
get
。您可以从它的
.prototype
,或通过使用
new
调用
main
创建的实例进行访问。第二,您可以将
.get
放在
main.prototype
上,但是
get
中的实例与
main
中的实例之间没有特殊关系。为什么要这样做?我想这样做,当有人看代码时,它看起来很漂亮。我认为
main.get.registration()
看起来比
main.prototype.get.prototype.registration()好看
@elclanrs-
这与保护无关