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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:18:48+00:00 2026-05-23T13:18:48+00:00

I have a JavaScript object those properties are static, mostly. They can be determined

  • 0

I have a JavaScript object those properties are static, mostly. They can be determined at construction time. However, I’ve also added a method “morph” that changes the object’s state. So these properties should change along with it.

I’ve coded it successfully below, as a method (longNameMethod, no problems) and as a property (longNameProperty, problematic). The problem with longNameProperty is that there is code in the constructor and in the morph method that looks very similar. Is there a way to eliminate this duplication?

var Fruit = function (name) {
    this.name = name;

    this.longNameMethod = function () {
        return this.name + this.name;
    }

    this.longNameProperty = this.name + this.name;

    this.morph = function(name) {
        this.name = name;

        // starting to feel redundant
        this.longNameProperty = this.name + this.name;
    }

    // update(); // hypothetical solution
};

var item = new Fruit('apple');

console.log(item.longNameMethod()); // apple apple
console.log(item.longNameProperty); // apple apple

item.morph('orange');

console.log(item.longNameMethod()); // orange orange
console.log(item.longNameProperty); // orange orange

I tried including an “update” method that would handle updating all of these properties but for some reason I can’t use it during construction. It says that this.name is undefined. What’s up with the order of operations during construction?

Edit: So yes, the method approach and the property approach are functionally identical to the outside, but the goal is to use the property approach.

Edit^2: So I think there are multiple issues at play… One of which is explained here: How does "this" keyword work within a function?

  • 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-23T13:18:49+00:00Added an answer on May 23, 2026 at 1:18 pm

    You need to add the method before using it, when you assign it into this:

    var Fruit = function (name) {
        this.morph = function(name) {
            this.name = name;
    
            this.longNameProperty = this.name + this.name;
        }
    
        this.morph(name);
    };
    
    var item = new Fruit('apple');
    
    console.log(item.longNameProperty); // apple apple
    
    item.morph('orange');
    
    console.log(item.longNameProperty); // orange orange
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails template (.rhtml file) generating a Javascript object. It looks something
There have been some questions about whether or not JavaScript is an object-oriented language.
I'm creating my own JavaScript Array-like object and I have methods that call closures.
In Javascript, I have an object: obj = { one: foo, two: bar };
Those of you proficient in Javascript and PHP know how to use the Object
I have a JavaScript method that I need to run on one of my
I have a javascript function that manipulates the DOM when it is called (adds
I have a JavaScript widget which provides standard extension points. One of them is
We have a JavaScript function named move which does just windows.location.href = any given
I have a JavaScript resource that has the possibility of being edited at any

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.