在函数内部创建方法
Create method inside function
我试图在函数内创建方法。我可以这样写:
function sample() {};
sample.show = function() { alert() };
我将看到呼叫sample.show();
的警报。但是为了美化代码,我想把所有的方法声明移到函数内部。我试过了:
function sample() {
sample.show = function() { alert() };
}
但我得到:TypeError: Object function sample() has no method 'show'
我试过的其他方法:
function sample() {
this.show = function() { alert() };
}
function sample() {
sample.prototype.show = function() { alert() };
}
function sample() {
this.prototype.method = function show () { alert() };
}
但结果相同。我甚至找不到任何关于在函数内部创建方法的信息。你能给我指条正确的路吗?
UPD:我想有能力调用sample()
函数,它也做一些事情。所以答案中还没有答案。
function sample() {
this.show = function() { alert() };
console.log('you called "sample()"');
}
sample(); // ==> you called "sample()"
首次尝试:
function sample() {
sample.show = function() { alert() };
}
这将只在sample
函数上创建一个"静态"方法,并且只在执行它之后
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() };
}
这只会在你创建一个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"方法,但是为了使原型链调用工作,你必须至少在手之前创建一个实例
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() };
}
将不工作,因为你不能直接从实例访问原型,一旦你试图创建一个实例,你会得到一个未定义的错误。
要使其工作,必须遍历构造函数链来设置
方法function sample() {
this.constructor.prototype.method = function show () { alert() };
//is the same as doing
sample.prototype.method = function show(){ alert() };
}
所以总的来说,为了你的"美化"工作,你必须首先调用sample
,要么直接调用第一个,要么为其他的创建一个实例。
示例函数中的代码在sample被调用之前不会被执行。所以你可以让你的例子工作通过这样做:
function sample() {
sample.show = function() { alert() };
}
sample()
sample.show()
你可以试试:
function sample() {
this.show = function() { alert() };
}
var x = new sample();
x.show();
你需要让sample成为一个对象
var sample = {
this.show = function() { alert() };
}
相关文章:
- 通过两种方法创建和更新 CSS
- javascript基对象,用不同的方法创建另外两个对象
- 为命名空间上公开的方法创建类型定义
- 尝试理解 JavaScript 中的对象和方法创建
- Meteor Braintree -- 通过 Meteor 方法创建客户端令牌
- 为嵌套在对象深处的方法创建别名
- 如何使用getter和setter方法创建一个Angular工厂而不遇到竞争条件
- 我们可以在 JavaScript 中使用 eval 方法创建 HTML 标签吗?
- 如何从原型方法创建对象的实例
- 为什么谷歌在其Javascript代码中为方法创建缩写
- 使用推送方法创建数组
- 如何访问使用jquery insertAfter方法创建的元素
- 如何通过objx的collect()方法创建json字符串特定字段的数组
- 如何从通过createHTMLDocument方法创建的文档中提取样式表
- 如何使用post方法创建重定向链接
- jQuery无法识别由.html()方法创建的选择器
- 为什么JavaScript中的顶级函数是作为window的方法创建的
- Javascript:通过不同的方法创建字符串
- 用方法创建javascript对象
- 在Javascript中使用Google calendar API插入方法创建新日历会返回错误400 "Mis