1

I am trying to log this in the console of Chromium browser

function.prototype.newMethod = function(name,f){
    this.prototype[name] = f;
    return this;
}

and I get "SyntaxError: Unexpected token ."

I don't see the issue. Can someone please help me resolve this.

Thanks!

3
  • 3
    function.prototype doesn't make any sense. function is a reserved keyword, not a type. Do you mean Function.prototype? Commented Jun 25, 2014 at 5:49
  • 1
    @JakeKing—"not a type" should be "not a constructor". Commented Jun 25, 2014 at 5:59
  • Oh goodness,, I don't understand how I missed that. Function is a constructor,, what a silly mistake. Thanks for pointing that out. Commented Jun 25, 2014 at 6:00

1 Answer 1

2

Use

Function.prototype.newMethod = function(name,f){
    this.prototype[name] = f;
    return this;
}

with a capital F since i'm assuming you're trying to make it so every instance of 'Function' has the 'newMethod' method. Not something you'd probably really want to do, but if you're just studying/trying things out go for it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Yes I am learning more about OOJS and how JavaScript works and trying to get better understanding of constructors over all. This is an exercise to write code that will enable adding of new method to any Constructor (for the sake of understanding how things work)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.