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

  • SEARCH
  • Home
  • 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 6964933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:59:16+00:00 2026-05-27T15:59:16+00:00

Is it possible in javascript to set properties inside methods? For example function Main()

  • 0

Is it possible in javascript to set properties inside methods?

For example

function Main() {

   this.method = function() {
      this.parameter = 'something_relevant'
   }
}

var p = new Main()
p.method()
console.log(p.method.parameter)

I tried this and it logs ‘undefined’. Is it about scope?

  • 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-27T15:59:17+00:00Added an answer on May 27, 2026 at 3:59 pm

    Inside method() you’re setting a property of the object on which the method is called rather than on the function object representing the method.

    This shows the difference inside the method:

    this.method = function() {
       this.parameter = 'abc'; // Set parameter on the object on which method() is called
       this.method.parameter = 'xyz'; // Set parameter on the object representing the method itself
    };
    

    This shows the difference in accessing the property after the method is called

    p.method();
    console.log(p.parameter); // Display property of the object p, equals 'abc'
    console.log(p.method.parameter); // Display property of the function object representing method(), equals 'xyz'
    

    You should decide whether you need a property on the function object or on p object. Note that the function object may be shared by a number of objects created by the Main() constructor. Hence it will behave in a manner somewhat similar to a static member in languages like C++ or Java.

    If you intend to use property defined on the object, your code should look similar to this:

    function Main() {
    
       this.method = function() {
          this.parameter = 'something_relevant'; // Set property on object on which method() is called.
       };
    }
    
    var p = new Main();
    p.method();
    console.log(p.parameter); // Read property from object p.
    

    If you intend to use a property defined on the function object representing method(), your code should look similar to this:

    function Main() {
    
       this.method = function() {
          this.method.parameter = 'something_relevant'; // Set property on function object representing method().
       };
    }
    
    var p = new Main();
    p.method();
    console.log(p.method.parameter); // Read property from the function object.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Is it possible to set default parameter values for functions in JavaScript like with
Possible Duplicate: Javascript closure inside loops - simple practical example Javascript: closure of loop?
Is it possible to set transparency of any image in javascript? And how can
Is it possible to set the height/width of an element in percent using JavaScript
Possible Duplicate: JavaScript: Why the anonymous function wrapper? I would like to ask you
Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic
Is it possible in javascript to assign an alias/reference to a local var someway?
Is it possible to set the CSS properties of a DIV to not initially

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.