Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:00:51+00:00 2026-06-12T22:00:51+00:00

using prototype method we can create new methods… like… Object.prototype.newMethod=function(){ // do something }

  • 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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:00:52+00:00Added an answer on June 12, 2026 at 10:00 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read here (Douglas Crockford) using prototype operator to add methods to Javascript classes
I would like to create a String.replaceAll() method in JavaScript and think using a
I like that in javascript, I can create a function, and then add further
I habe read here about defining method for a Javascript class Advantages of using
Using JavaScript, is it possible to use prototype to add a method to a
So I decided to start using prototype and here's my first question. I'm trying
I have created one multiplication of complex function using prototype in java-script function ComplexNumber(real,imaginary)
I have a file that looks like this: function UserController(){}; UserController.prototype = { theData:
I can declare methods of an object in two ways: The first way uses
I cooked up a pattern to create and extend html elements using their prototype.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.