JavaScript Can I change a javascript variable within another variable? change javascript variable to php,change javascript variable in browser,

y is evaluated when you set z. At that time y is 6 so z is 7. When you increase y then z is not re evaluated (as you've found out).

y=1;
z=++y;//increment y and assign the value to z
 // z will be 2;
z=y++;//set z to the value of y and then increment y
   // z is now 2 and y is 3

If you want one variable to be dependent on the value of another variable you can't just assgin new values to them. You have to use getters and setters functions:

var myObj={
  y:0,
  x:0,
  setY:function (value){
    this.y=value;
    this.x=value+1;
  },
  setX:function (value){
    this.x=value;
    this.y=value-1;
  }
}
myObj.setY(4);
console.log(myObj.x);//=5
myObj.y=2;//breaks setting x, you have to use setters
console.log(myObj.x);//=5

As you can see the line myObj.y=2 breaks setting z so you can't assign values to either myObj.y or myObj.z without breaking it.

To prevent this from happening you have to make x and y private. In JavaScript you can simulate private variables using closures.

A warning for the folling code: if you plan to create multiple instances of your object use constructor functions and have private variables using code conventions (like _privateVar) instead of real privateness since JS doesn't support it unless you're planning on not using prototype.

var myObj=(function(){
  var x=0;// x and y only exist in the funciton body
  var y=0;// you cannot access them in myObj.x or .y
  return{
    setX:function(value){
      //maybe check if value is a number here
      x=value;
      y=value-1;
    },
    setY:function(value){
      //maybe check if value is a number here
      y=value;
      x=value+1;
    },
    getX:function(){
      return x;
    },
    getY:function(){
      return y;
    }
  }
})();
myObj.setX(6);
console.log(myObj.getY());//logs 5
myObj.y=22;
console.log(myObj.getY());//still logs 5

Using bfavaretto's syntax with get and set you can assign new values but internally JavaScript will use the getters and setters functions. This does not work in older browsers like IE8 and under.

var myObj={
  _y:0,
  _x:0,
  set y(value){
    this._y=value;
    this._x=value+1;
  },
  get y(){
    return this._y;
  },
  set x(value){
    this._x=value;
    this._y=value-1;
  },
  get x(){
    return this._x
  }
}
myObj.x=4;
console.log(myObj.y);//=3
Answer:1

One way to do it is to wrap the variable in an object, which is passed by reference rather than by value:

var obj = { y : 6 };
var obj_2 = obj;
alert(obj.y); //6
alert(obj_2.y); // 6
obj.y++;
alert(obj.y); // 7
alert(obj_2.y); // 7`
Answer:2

I have created a listview in jquery with a listdivider with a filter. The filter works as expected but as soon as you collapse either of the list dividers, the search subsequently does not work at all,...

I have created a listview in jquery with a listdivider with a filter. The filter works as expected but as soon as you collapse either of the list dividers, the search subsequently does not work at all,...

  1. jquery mobile collapse
  2. jquery mobile collapse dynamically
  3. jquery mobile collapse div
  4. jquery mobile collapse navbar
  5. jquery mobile expand collapse
  6. jquery mobile expand collapse programmatically
  7. menu collapse jquery mobile
  8. jquery mobile collapsible set collapse all
  9. jquery mobile expand collapse div

I have a PHP Function that I would like to integrate into my (existing) web page. Further, I would like it to execute when the user clicks a link on the page. The function needs to accept the text ...

I have a PHP Function that I would like to integrate into my (existing) web page. Further, I would like it to execute when the user clicks a link on the page. The function needs to accept the text ...

  1. calling function through pointer

I have a form in Angular setup like the following. Users can make changes and submit one at a time and it works as expected but I'd like to add a "Submit All" that would submit each item one at a ...

I have a form in Angular setup like the following. Users can make changes and submit one at a time and it works as expected but I'd like to add a "Submit All" that would submit each item one at a ...

  1. submit multiple forms with one button
  2. submit multiple forms with one button powerapps
  3. submit multiple forms with one button jquery
  4. submit multiple forms with one button angular
  5. submit multiple forms powerapps
  6. submit multiple forms with one button ajax
  7. submit multiple forms with one button django
  8. submit multiple forms with one button php
  9. submit multiple forms with ajax
  10. submit multiple forms with one button angular 2
  11. submit multiple forms with one button asp.net mvc
  12. submit multiple forms
  13. submit multiple forms with one button html
  14. submit multiple forms with one button jquery ajax
  15. submit multiple forms at once
  16. submit multiple forms with one button rails
  17. submit multiple forms php
  18. submit multiple forms jquery
  19. submit multiple forms django
  20. submit multiple forms using ajax

I am trying to detect Hebrew characters using a regular expression. From what I have read, this is any character in the Unicode range 0x0590-0x05ff. However, the following doesn't work: // ...

I am trying to detect Hebrew characters using a regular expression. From what I have read, this is any character in the Unicode range 0x0590-0x05ff. However, the following doesn't work: // ...

  1. hebrew unicode range