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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:43:57+00:00 2026-06-10T21:43:57+00:00

I’m having difficulties understanding behavior of Javascript. Code: function getPosition(element){ var position = {

  • 0

I’m having difficulties understanding behavior of Javascript.

Code:

function getPosition(element){
    var position = {
        x:$(".line div").has(element).index(),
        y:$(".line").has(element).index()
    };
    console.log(position.y);
    console.log(position)
    return position;
}

Now while calling it from function I’m getting results like this:

0

Object
x: 8
y: 3

What I don’t understand is how is it possible to change object attributes when trying to access it via object reference, but not directly.

But when I call same function from console I get this:

0

Object
x: 8
y: 0

This is the same element passed to function. And it seems that it fails always when X or Y is 0(zero), when it’s another number it’s ok.

Could anyone explain what I’m doing wrong? Or is it somekind of JS bug? O_o

EDIT:

So I finally found out what the problem was. I always thought that I was passing values but unfortunately I was wrong all the time. During some searches on stackoverflow, I found topic about JS values and references.

If anyone is interested and too lazy to read the topic, you can look at this example. It’s pretty much self explanatory.

function test(){
    var a = 5;
    var b = a; //b now has value of 5
    console.log("a:"+a+":b:"+b);
    b = 4;//a still has value of 5 and b is assinged to 4
    console.log("a:"+a+":b:"+b);
    var c = {val:1};
    var d = c; //d now has reference to c
    d.val = 2; //c.val changes because it is a reference
    console.log(c);
}

EDIT2:
oh and by the way, how can I mark my question as answered?

  • 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-10T21:43:59+00:00Added an answer on June 10, 2026 at 9:43 pm

    So I finally found out what the problem was. I always thought that I was passing values but unfortunately I was wrong all the time. During some searches on stackoverflow, I found topic about JS values and references.

    If anyone is interested and too lazy to read the topic, you can look at this example. It’s pretty much self explanatory.

    function test(){
        var a = 5;
        var b = a; //b now has value of 5
        console.log("a:"+a+":b:"+b);
        b = 4;//a still has value of 5 and b is assinged to 4
        console.log("a:"+a+":b:"+b);
        var c = {val:1};
        var d = c; //d now has reference to c
        d.val = 2; //c.val changes because it is a reference
        console.log(c);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.