1

I'm just trying to get a "getter" in javascript in this way

var Product = function(){
  var self = this; 
  var _id = function() { 
    return self.styleCode + '-' + self.materialCode + '-' + self.colourCode; 
  }
  self.materialCode = ''
  self.colourCode = ''
  ..
  self.id = _id()
}
..
var obj = new Product();
//.. initialize properies
obj.id // = "--"

But it seems doesn't call the function every time as I believed. I tried to use this Javascript: Use function as variable something like this

var Product = function(){
  var self = this; 
  self.materialCode = ''
  self.colourCode = ''
  ..
  get self.id() { return self.materialCode + '-' + self.colourCode; }
}   

but I get an exception on the get keyword. Any idea? Thanks

5
  • get self.id() { ... } --- what does this code mean? Commented Jul 30, 2015 at 12:42
  • get is not cross-browser. This reserved word don't run in all browsers. I try to provide you another solution, I'm searching for it Commented Jul 30, 2015 at 12:43
  • @zerkms see the link in the post Commented Jul 30, 2015 at 12:44
  • @MarcosPérezGude it works in all modern browsers Commented Jul 30, 2015 at 12:44
  • If you look more closely, this syntax is used in an object initializer; I don't know if it will work in a function declaration. Also, the browser has to support it. Commented Jul 30, 2015 at 12:45

4 Answers 4

4

You can define the id property on self, with Object.defineProperty, like this

Object.defineProperty(self, 'id', {
    get: function () {
      return self.materialCode + '-' + self.colourCode;
    }
});

You can see this example from MDN, for reference.

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

4 Comments

Thanks this works, is there any browsers compatibility issue ?
@DevT: Yes, it's not supported IE8 and below. See MDN-article.
@Mouser this isn't good.. so this is the better way to do a getter but it isn't supported on IE8 and below.
@DevT, yep, modern websites should't support those older browsers anymore. Windows 10 is coming. Time to wave goodbye to all IE browsers before IE10. However get and set is supported on DOM nodes in IE8.
3

Use Object.defineProperty on MDN.

var Product = function(){
  var self = this; 
  self.materialCode = ''
  self.colourCode = ''

  Object.defineProperty(self, "id", {
        //printing the full code here
        get : function(){return self.materialCode + '-' + self.colourCode;},
        set : function(value){ null },
        enumerable : false,
        configurable : false
        //this is the default set up for Object.defineProperty when using getters and setters,
        //those two other properties default to false.
  });
}

var obj = new Product();
obj.materialCode = 124;
obj.colourCode = 5783;
alert(obj.id);

Just invoke with obj.id.

You can set the set and get function. And use it as a normal property declaration. I urge you to read the article on MDN to learn more about the possibilities.

Comments

0

getter and setters are made in different way

with this code

self.id = _id();

you are storing in self.id return value of _id() called in constructor

with this code

get self.id()

you are declaring a getter with a parameter (correct way get id())

here you can find usefull information about getter/setter and don't forget firefox GETTER and SETTER

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get

keep warning! this code do not work in non-firefox browsers!

Comments

0

Is another way

var xvar = new function(id){ 
    this.toString = this.valueOf = function() {
         return $(id).width();
    };
}('#y');

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.