最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create method inside function - Stack Overflow

programmeradmin3浏览0评论

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

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

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() };
}

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: 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()"

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

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

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() };
}

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: 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()"
Share Improve this question edited Mar 20, 2014 at 18:27 Vlad Holubiev asked Mar 20, 2014 at 18:14 Vlad HolubievVlad Holubiev 5,1547 gold badges46 silver badges61 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 12

First attempt:

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"

Second Attempt:

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

Now with the prototype version:

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

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

However the last one:

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() };
}

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.

the code in the sample function will not be executed until sample is called. So you could get your example to work by doing this:

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

sample()
sample.show()

You can try this:

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

var x = new sample();
x.show();

you need to make sample an object

var sample = {
  this.show = function() { alert() };
}
发布评论

评论列表(0)

  1. 暂无评论