1

I was wondering if what is possible in javascript with cookies is doable somehow in node.js for example

document.cookie="sss=fff;";

i want to define a method in the an object that behaves like this, and instead of making the variable equal to the string it pushes it in an array.

function o(){
this.add=[]
}
var o1= new o();
o.add="sss=fff;";
3
  • Arrays support push(). Unless I misunderstand, o.add.push("sss=fff;") will do what you want. EDIT: Oh I see, you want the assignment to behave like a push. Commented Jan 14, 2014 at 0:07
  • I know, but i am talking about the syntax. Commented Jan 14, 2014 at 0:09
  • This is a really neat question. It looks like Object.watch or Object.observe will allow you to listen to variable changes. I suppose you could make a callback that pushes onto the array when you try to modify it. This might be of use: updates.html5rocks.com/2012/11/…. Commented Jan 14, 2014 at 0:23

1 Answer 1

3

Operator Overriding vs. Getter and Setter

There is no way to override operators in JavaScript. = is always and only used for assignment. You can on the other hand use getter and setter definitions on an object to perform special operations on values before they are set or retrieved.

Reference: http://ejohn.org/blog/javascript-getters-and-setters/

Example:

function o(){
    values = [];
    this.__defineSetter__("add", function(v){
        values.push(v);
    });
    this.__defineGetter__("add", function(){
        return values.join("");
    });

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.