-1

How can I use property : value pairs in object contexts as if they were var = value pairs? So without using the this keyword?

var obj = {
    prop_1 : 1,
    prop_2 : 2,
    myMethod : function(){
        this[this.prop_1] = this.prop_1;
        return prop_1;
    }
}

console.log( obj.myMethod() );
Puddingboy
  • 127
  • 9
  • Can you explain a bit more? Are you trying to create `getter` methods without using `this`? – Rajesh Jan 07 '16 at 13:03
  • 1
    in your code, changing `this` to `obj` and adding `obj.` in the `return` statement makes everything "work" - though, as this is probably an over simplified example of a "real world" problem, not sure if this helps at all – Jaromanda X Jan 07 '16 at 13:05
  • I think I don't jus want to get those variables but also dynamically reset them? What would be the right way to do something simular? – Puddingboy Jan 07 '16 at 13:06
  • 1
    I think you may need to expand the question somewhat, as it stands it's vague at best – Jaromanda X Jan 07 '16 at 13:07
  • Is `with` what you're looking for? https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/with – haim770 Jan 07 '16 at 13:07
  • As @JaromandaX said, in that code, just use `obj.prop_1`. The `return` currently throws, as well, as there's no `prop_1` variable in scope; you'll want `return obj.prop_1;`. – T.J. Crowder Jan 07 '16 at 13:08
  • `this[this.prop_1] = this.prop_1;` sets a property with the name from `this.prop_1` (`1` in the above) to the value from `this.prop_1` (`1` in the above). Is that really what you meant to do? – T.J. Crowder Jan 07 '16 at 13:09
  • in this question i made a quick explanation for this. http://stackoverflow.com/questions/34422418/execution-context-in-javascript/34422645#34422645 – Denis Jan 07 '16 at 13:09
  • @T.J.Crowder when you say `just use obj.prop_1` - shouldn't `this[..]` also be `obj[..]` - and I did mention the obj. requirement in the return :p – Jaromanda X Jan 07 '16 at 13:10
  • @JaromandaX: If I were to use obj[]. What would happen to obj2 if I were to do something like this: `var obj2 = new obj`; – Puddingboy Jan 07 '16 at 13:14
  • @Puddingboy: `new obj` would be an error, `obj` isn't a function. You can only use functions with `new`. – T.J. Crowder Jan 07 '16 at 13:19
  • @JaromandaX: I just meant `obj` rather than `this`. I couldn't make out your comment at first, I've figured it out now. The `obj.` tricked my eye into thinking you were starting a new sentence about the return (which didn't make any sense). – T.J. Crowder Jan 07 '16 at 13:22
  • @T.J.Crowder - if I can't confuse at least one person I'm not trying hard enough :p – Jaromanda X Jan 07 '16 at 13:22
  • I'm not really being all that vague. The reason I want to use my objects `property:value` pairs and treat them as `variable = value` pairs(in this scope atleast) is because it would shorten the code significantly and decrease the amount of `this` references. By now I have figured out that this idea itself is frowned upon. If I shouldn't want this. What should I want? – Puddingboy Jan 07 '16 at 13:40
  • @Puddingboy: If I'm reading that right, then haim770 was right about `with`. But I wouldn't, it gets really confusing, really fast. Instead, just use a short local alias, e.g. `var t = this`. `t.` isn't that long. – T.J. Crowder Jan 07 '16 at 15:31
  • The use of the with keyword is commonly recommended against, so I'll just work with abrivations. – Puddingboy Jan 07 '16 at 16:21

2 Answers2

0

You can use function as a class.

Edit 1

Remove new credits @Denis

function myClass(){
  // Private variables
  var prop1 = 10;
  var prop2 = 20;
  
  function getProp1(){
    return prop1;
  }
  
  function getProp2(){
    return prop2;
  }
  
  function setProp1(value){
    prop1 = value;
  }
  
  function setProp2(value){
    prop2 = value;
  }
  
  // Public property...
  return {
    getProp1:getProp1,
    getProp2:getProp2,
    setProp1:setProp1,
    setProp2:setProp2,
  }
}

var obj = myClass();

console.log(obj.getProp1());

obj.setProp1(100);

console.log(obj.getProp1());
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • 2
    you should not call new myClass(); regular call is enough like myClass(); because when you call with new you create a new object and since your function return it's own object you just throw away that fresh new one. – Denis Jan 07 '16 at 13:16
  • Might be easier to just create those methods as properties on the class (```this.setProp1 = function(){}```). – rodrigo-silveira Jan 07 '16 at 13:18
  • looks like topic starter doesn't have clear understanding of what is THIS so he want to find a way to avoid using it =) – Denis Jan 07 '16 at 13:20
  • "without using the this keyword" Wrote answer based on this line in question – Rajesh Jan 07 '16 at 13:22
0

If you want to reduce instances of the key word this inside myMethod, the best I would offer you is to replace this with t or something of that sort. This would only add two characters to the original variable name.

myMethod: function() {
   var t = this;

   // use t instead of this
   t.prop_1 = 43 - t.prop_1;

   return t.prop_1;
}

Here's a JSFiddle

guysigner
  • 2,822
  • 1
  • 19
  • 23