在函数内创建方法 [英] Create method inside function

查看:49
本文介绍了在函数内创建方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在函数内创建方法。我可以这样做:

I am trying to create method inside a function. I can make this such a way:

function sample() {};
sample.show = function() { alert() };

我会看到呼叫 sample.show(); 。但是由于代码美化的原因,我想在函数内部移动所有方法声明。我试过了:

And I'll see the alert calling sample.show();. But for the reason of code beautifying I want to move all method declarations inside functions. I tried that:

function sample() {
    sample.show = function() { alert() };
}

但我得到: TypeError:对象函数示例( )没有方法'show'
我尝试的另一种方式:

But I get: TypeError: Object function sample() has no method 'show' Another ways I tried:

function sample() {
    this.show = function() { alert() };
}
function sample() {
    sample.prototype.show = function() { alert() };
}
function sample() {
    this.prototype.method = function show () { alert() };
}

但结果是一样的。我甚至找不到有关在函数内创建方法的任何信息。你能指出一个正确的方法吗?

But result was the same. I can't even find any information about creating methods inside functions. Can you point me a right way?

UPD :我希望能够调用 sample()函数也可以做一些事情。所以答案中还没有解决方案。

UPD: I want to have ability to call sample() function which also does some stuff. So there are no solution in answers yet.

function sample() {
    this.show = function() { alert() };
    console.log('you called "sample()"');
}
sample(); // ==> you called "sample()"


推荐答案

首次尝试:

function sample() {
    sample.show = function() { alert() };
}

这只会在示例函数,仅在执行后

This would only create a "static" method on the sample function and only after executing it

console.log(sample.show);
//would show undefined on the console
sample();
console.log(sample.show);
//would then show the method definition as it has now been
//defined as a property of "sample"

第二次尝试:

function sample() {
    this.show = function() { alert() };
}

这只适用于创建样本实例

This will only work if you create a instance of sample

console.log(sample.show);
//will print undefined as there is no property "show" on sample
sample();
console.log(window.show);
//will print the function, but since sample was executed without a
//context, show is created as a property of the global object
//in this case "window"
var d = new sample();
console.log(d.show);
//will print the function as we now have a instance of sample which
//show was made a property of
console.log(sample.prototype.show);
//will show undefined as you are not actually creating the "show" method
//on the constructor sample's prototype

现在有原型版本:

function sample() {
    sample.prototype.show = function() { alert() };
}

通过这个,您可以通过访问方法访问show方法实例或原型链,但对于原型链调用工作,你必须至少先做一个实例

With this after you will be able to access the "show" method either through the instance or from the prototype chain, but for the prototype chain call to work you have to at least make one instance before hand

console.log(sample.prototype.show);
//will print undefined as "show" has not been defined on the prototype yet
var d = new sample();
console.log(d.show);
console.log(sample.prototype.show);
//Both will now print the function because the method has been defined

然而最后一个一个:

function sample() {
    this.prototype.method = function show () { alert() };
}

完全不起作用,因为你无法直接从实例访问原型一旦你尝试创建一个实例,你将得到一个未定义的错误。

Will not work at all as you can not access the prototype directly from the instance you will get a undefined error once you try to create an instance.

要使它工作,你必须通过构造函数链来设置方法

For it to work you would have to go through the constructor chain to set the method

function sample() {
    this.constructor.prototype.method = function show () { alert() };
    //is the same as doing
    sample.prototype.method = function show(){ alert() };
}

总的来说,为了美化工作,你必须打电话给 sample 首先,直接,第一个,或者为其他人创建一个实例。

So overall for your "beautifying" to work you have to call sample first, either directly, for the first one, or by creating an instance for the others.

这篇关于在函数内创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆