0

using prototype method we can create new methods... like...

Object.prototype.newMethod=function(){
   // do something
}

Here I am defining the newMethod with an anonymous function... now if I want to use this method, I have to use it like: <object>.newMethod();

But now I want to create a new method which I can use like: <object>.newMethod;... no brackets... How can I do that...??

please don't use any jQuery...

6
  • 1
    Simple answer: Don't do that. Commented Oct 17, 2012 at 15:09
  • If you want to pass a reference to that method, you can just use <object>.newMethod, no magic needed. Otherwise I'm not sure what you want to do... do you want to execute a method without calling it? Commented Oct 17, 2012 at 15:10
  • I need to trigger the anonymous function using newMethod instead of newMethod() Commented Oct 17, 2012 at 15:12
  • Why is the non-use of () necessary/important? Commented Oct 17, 2012 at 15:14
  • actually I want to build a method like <object>.parentNode... where we don't write parentNode()... only parentNode... no ()... Commented Oct 17, 2012 at 15:17

3 Answers 3

5

Erm, you can't. To call a method, you write parentheses after it. Otherwise you're just referencing it.

The only exception to this rule is when you write something like new Date, where the parentheses are implict due to the new keyword and only because there are no arguments given.

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

Comments

1

I can't really understand why you would want to do that, but it is possible, albeit with a nasty hacky workaround. What you're actually looking for, AFAIK, is a magic property (like the someArray.length property).

var foo = {val:'foo'};
foo.length = (function(that)
{
    return function()
    {
        return that.val.length;
    }
})(foo);
//at this point foo.length(); returns 3, but still requires parentheses
//so, build another closure, and assign a valueOf method to the lenth method:
foo.length.valueOf = (function(method)
{
    return function()
    {
        return method();//call the length method
    }
})(foo.length);
console.log(foo.length +1);//logs 4
foo.val += 'bar';
console.log(foo.length);//logs 6
//BUT:: be carefull!!
alert(foo.length);//coerces to string, we haven't redefined the toString method, so the function code will be alerted
alert(foo.length + '');//alerts 6

This is just to show you that, yes it is theoretically possible, but please, please, don't use this kind of overly polluted hacks... I haven't thoroughly tested this, but ATM, I've already noticed that console.log(foo.length); can return a different value, not sure why, yet:

foo = {val:'foo'};
foo.length = (function(that){return function(){ return that.val.length;};})(foo);
foo.length.valueOf = (function(method){return function(){return method();};})(foo.length);
foo.length;//returns 3, great
foo.val += 'bar';
console.log(foo.length);//logged 3 at first, now it's back to logging 6!<-- don't trust this is the conclusion

2 Comments

It's the name of an argument, passed to an IIFE, creating a closure... Put simply that === foo. Because (function(that){})(foo);<-- I declare a function, and invoke it. The function I declared accepts 1 argument, called that, and I pass foo as its value. The return value of this IIFE is assigned to foo.length. This return value is a function, that has "immutable access" to foo. It's done because this might end up referencing another object, depending on the context, that will always point at foo. Same logic applies to method (Immediately Invoked Function Expression)
thanks for your comment @Elias Van Ootegem... I thought that is something like this... now that is clear to me...
0

The only way to call a function without parenthesis would be to define it using getters and setters.

Note these are new to JavaScript 1.8 and are not supported by all browsers.

Comments

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.