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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:16:00+00:00 2026-06-08T21:16:00+00:00

I have this setter, but I don’t know why set value and it doesn’t

  • 0

I have this setter, but I don’t know why set value and it doesn’t change :

    this.setHead = function(head){
        console.log('Head: x:'+this.getHead().getX()+' y:'+this.getHead().getY());
        console.log('temp Head: x:'+head.getX()+' y:'+head.getY());
        this.head = head;
        console.log('Head: x:'+this.getHead().getX()+' y:'+this.getHead().getY());
    }

And the result in Chrome log is :

Head: x:5 y:10 // old value
temp Head: x:1 y:7 //temporary value decide to copy
Head: x:5 y:10     // and the new valụe : NO CHANGE

I have read that Javascript pass object by reference, I don’t know does it difference with Java. If not, I don’t know why happen that. Please tell me.

Thanks 🙂

@ Edited : I have add a line for log, and see strange result :

console.log('Head: x:'+this.head.getX()+' y:'+this.head.getY());
Head: x:1 y:7 

It’s strange, because I think it should be same with below line but It don’t

console.log(‘Head: x:’+this.getHead().getX()+’
y:’+this.getHead().getY());

And my getHead() is :

this.getHead = function() {
            return head;
}
  • 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-08T21:16:02+00:00Added an answer on June 8, 2026 at 9:16 pm

    Javascript doesn’t pass anything by reference, setting this.head doesn’t magically make head refer to something else (which is what by reference implies)

    Your .getHead() method returns head, not this.head, so the assignment doesn’t affect getHead() at all. They refer to different objects.

    Try this:

    this.getHead = function() {
        return this.head;
    }
    

    Basically what you have is most likely:

    function Ctor( head ) {
    
        this.getHead = function() {
            return head;
        }
    
        this.setHead = function( head ) {
            this.head = head;
        }
    
    }
    

    the setHead sets object property, where as getHead returns the head variable passed to the constructor when initialized.

    To avoid this confusion you should just stick to object properties and prototypes and it’s all very simple:

    function Ctor( head ) {
        this.head = head;
    }
    
    Ctor.prototype.getHead = function() {
        return this.head;
    };
    
    Ctor.prototype.setHead = function(head) {
        this.head = head;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this style: <Style x:Key=SelectableListBoxItemStyle TargetType={x:Type ListBoxItem}> <Setter Property=Template> <Setter.Value> <ControlTemplate TargetType={x:Type ListBoxItem}>
So I have this WPF style: <Style x:Key=SmallLinkButton TargetType=Button> <Setter Property=Template> <Setter.Value> <ControlTemplate TargetType=Button>
I don't know why I started thinking about this, but now I can't seem
I have a setter like this: - (UIImagePickerController *) foto { if (_foto ==
I have a class defined like this, with the appropriate getter and setter methods...
I have a Listview, such this: <ListView ItemsSource={Binding ...} > <ListView.ItemContainerStyle> <Style TargetType=ListViewItem> <Setter
I have some extra space around my Listbox. It's 1px wide, but I don't
In the XAML provided below, I don't have the value for Phone sometimes. When
Sorry for the terrible Question Title, I don't know how to phrase this, feel
I have a bean Config that has a required setter. However, when I don't

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.