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 7033931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:07:28+00:00 2026-05-28T01:07:28+00:00

I am looking for correct way to add static methods (class level methods) into

  • 0

I am looking for correct way to add “static” methods (class level methods) into Object and all its children in JavaScript.

I am trying to do:

Object.prototype.myMethod = function() { ... };  
// actually I need only class level method
// but it creates instance method too

I am getting wanted:

function MyClass() {};
var mc = new MyClass();

mc.myMethod();

But I am getting also unwanted:

mc.someProperty = 'val';

for (var k in mc) {
  console.log(k);
}

// => myMethod (this is unwanted)
// => someProperty

How to add only class method into Object and all it’s children without adding of instance method’s?

  • 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-05-28T01:07:29+00:00Added an answer on May 28, 2026 at 1:07 am

    The code is correct, your loop has to be altered:

    for (var k in mc) {
        if (mc.hasOwnProperty(k)) {// Ignores inherit methods/properties
           console.log(k);
        }
    }
    

    An alternative solution is by using the Object.defineProperty method to make the element not-enumerable:

    Object.defineProperty(mc.prototype, 'myMethod', {
        value: function() {
            // Logic here
        },
        enumerable: false, // <--- Property unreachable through for(k in mc)
        writable: true,    // <-- Optional, default false
        configurable: true // <-- Optional, default false
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for the correct way and control to import and export of out of
I'm looking for (arguably) the correct way to return data from a XmlHttpRequest .
I am looking to the fastest and the correct way to check if a
I am looking at singletons and I was curious about the correct way to
Looking at posts like this and others, it seems that the correct way to
I'm looking at this code of the correct way to do a singleton in
Im looking for a way to replace the following: public class NonTypeSafe { private
I am looking for a (correct) way to read and write to a linux
What's the correct way to resign the current firstResponder? I've seen the following: Looping
I am looking at code which has the correct using statements declared but yet

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.