Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JS have getters and setters methods named same as property?

I want to achieve behaviour like down in code:

function Foo(name) {
    this.name = name;
};

var myFoo = new Foo('myName');

myFoo.name('newMyName'); // sets myFoo.name = 'newMyName'
myFoo.name(); // returns 'myName'

But it is obvious that in that case I'm overridding name property with name function. Is anyhow possible to achieve that functionality?

like image 785
Alan Kis Avatar asked Oct 23 '13 02:10

Alan Kis


People also ask

Can getters and setters have the same name JavaScript?

You can only have one getter or setter per name, on an object. (So you can have both one value getter and one value setter, but not two 'value' getters.) The only way to delete a getter or setter is to do: 'delete object[name];' Be aware, that this command is capable of deleting normal properties, getters and setters.

Do getters and setters count as methods?

Getter and Setter are methods used to protect your data and make your code more secure. Getter returns the value (accessors), it returns the value of data type int, String, double, float, etc.

What is the benefit of using properties with getters and setters?

The getter and setter method gives you centralized control of how a certain field is initialized and provided to the client, which makes it much easier to verify and debug. To see which thread is accessing and what values are going out, you can easily place breakpoints or a print statement.

What is property getters and setters?

What are Getters and Setters? Getters: These are the methods used in Object-Oriented Programming (OOPS) which helps to access the private attributes from a class. Setters: These are the methods used in OOPS feature which helps to set the value to private attributes in a class.


1 Answers

When talking about getters and setters in javascript you may be talking about one of two concepts:

1. Getters and setters as a concept like in any other OO language.

This is the case illustrated by the code in your question. In this case, an object's property is simply that, a property which may or be either an object or a function. javascript keeps track of both within the same namespace. Indeed, functions are just objects in javascript so there is no concept of a separate namespace for functions like you'd find in languages like C.

In this case "getters" and "setters" are just regular functions so the value needs to be stored separately. There are several strategies around this.

One is to use implicit getSomething() and setSomething() style functions commonly found in Java. This allows you to disambiguate the getters and setters from the property name since the getters and setters have the word "get" and "set" added to the name.

The second strategy is the one you've written in your question. In this case you need to store the property in another name so as not to share the same name with the getter/setter.

The third strategy is to store the value in a closure:

    function Foo (name) {
        var name = name;
        this.name = function (str) {
            if (str !== undefined) name = str;
            return name;
        }
    }

Note that in the code above the value is stored in name but the getter/setter is this.name which is a completely different variable. This allows your example code to work as you expected:

    var me = new Foo('Mark');
    me.name(); // returns Mark
    me.name('Andy'); // sets name to Andy

2. Getters and setters as a mechanism in javascript.

This is a feature of newer versions of javascript that follows the ECMAscript 5 specification. This feature allows properties to execute code when reading or writing to it similar to how the .innerHTML property of DOM object invokes the HTML parser when you assign something to it.

The syntax of getters and setters is similar to functions but introduces the get and set keywords in place of function.

A simple example of a property with getter and setter:

    var me = {
        first_name : "",
        last_name : "",
        get name() {
            return this.first_name + " " + this.last_name;
        },
        set name(str) {
            var n = str.split(/\s+/);
            this.first_name = n.shift();
            this.last_name = n.join(' ');
        }
    }

The code above allows you to treat the functions to get and set the first_name and last_name as if it is a variable instead of a function. To use the name getter and setter you'd simply do:

    me.name = "James Bond";
    alert(me.first_name); // should alert James
    alert(me.last_name); // should alert Bond
    me.last_name = "Dean";
    alert(me.name); // should alert James Dean

Using the javascript get/set mechanism, you can't store the value in the object using the same name. For example:

    var foo = {
        set bar(x) {this.bar=x}
    }

The code above will compile but trying to set bar: foo.bar = 1 will cause a stack overflow because of the infinite loop - the this.bar= inside the setter will call the setter again.

like image 84
slebetman Avatar answered Oct 19 '22 09:10

slebetman