0

What I would like to have is something like this:

var fnc = new constructor(); // --> function()...
fnc(); // --> Running main function
fnc.method(); // --> Running method

So that one could create new fnc instances, while having the methods in the constructors prototype. However, I don't seem to be able to have result of the constructor as a function, as regardless of the return clause, it seems to become a object, resulting to ..

Uncaught TypeError: Property 'fnc' of object [object Object] is not a function 

:(

Edit: Added example code. So what I'm thinking is along these lines.

var constructor = function() { return function() { console.log('Running main function.') }; };
constructor.prototype.method = function() { console.log('Running method.'); }

var fnc = new constructor();

console.log('constructor: ', constructor);
console.log('fnc:         ', fnc);
console.log('fnc.method:  ', typeof $.method);
console.log('');
fnc();
fnc.method();

In this case, one gets the...

Uncaught TypeError: Object function () { console.log('Running main function.') } has no method 'method' 

...error as the function provided by the constructor is different than the constructor itself, on which prototype chain is tacked on. Just can't seem to wrap my head around this. Did one version with jQuery like initialization, which did the trick alright but I later on found out that it ended up doing it through the empty function prototype, meaning every function got the methods.

2
  • Can you show the implementation code you have so far? The three lines only show how you want it to behave, not how you've made it behave so far. Commented Aug 15, 2013 at 13:02
  • Show us your code that is throwing this error. fnc is not a property in what you presented us. Commented Aug 15, 2013 at 13:10

3 Answers 3

2

You can not create callable objects from constructor function instances.

You could have a function that returns a callable function that has methods assigned to it:

function foo() {
    function fn() {
        ..do stuff..
    }
    fn.method = function () {
        ..do more stuff..
    };
    return fn;
}
var bar = foo();
bar(); //does stuff
bar.method(); //does more stuff

You could even call the function as a constructor, but it would actually be doing the same thing as calling the function directly as a function, and not be constructing a new instance of the class:

var baz = new foo();
baz(); //does stuff
baz.method(); //does more stuff
baz instanceof foo; //false
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's one way of doing but it doesn't use prototyping, meaning every new fnc would create their own functions instead of using the ones up the prototype chain. When you have lot of methods, lot of fnc's and a low end device, it's not ideal. :/
-2
function Constructor(n,s,r) {
this.var1 = n;
this.var2 = s;
this.var3 = r;
}

var fnc = new Constructor("Hi", 3, 6);

fnc.prototype.myMethod = function() {
console.log (this.var1);
}

1 Comment

This doesn't answer the question.
-2

You can create methods specifically to objects like this

function myObject(name)
{
   this.name = name;
}

myObject.prototype =
{
   getName: function()
   {
      console.log("Name is: " + this.name);
      return this.name;
   }
}

var newObj = myObject("javascript");
var objname = newObj.getName();
console.log("Name is again: " + objname);

1 Comment

This does not address the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.