Javascript Getters/Setters

by eddhie

JavaScript

function aaa(){
	return _ethnicity;
}
function Person(name) {

    var _ethnicity;
    var _religion;
    var _name;
    var _children = [];


    //constructor
    if (! function (args) {
        if (args.length != 1) {
            return false;
        }
        name = name.toUpperCase();
        _name = name;

        return true;
    }(arguments)) {
        throw new Error("Instantiation Failed");
    }



    Object.defineProperty(this, "ethnicity", {
        get: aaa(),
        set: function (val) {
            _ethnicity = val;
        },
        enumerable: true,
        configurable: false
    });

    Object.defineProperty(this, "religion", {
        get: function () {
            return _religion;
        },
        set: function (val) {
            _religion = val;
        },
        enumerable: false,
        configurable: false
    });


    Object.defineProperty(this, 'name', {
        value: _name,
        writable: false,
        enumerable: true,
        configurable: false
    });


    Object.defineProperty(this, 'children', {
        value: _children,
        writable: false,
        enumerable: true,
        configurable: false
    });
    this.addChild = addChild;

    function addChild(name, bday, bio) {
        _children.push({
            name: name,
            bday: bday,
            bio: bio
        });
    }


    //THIS WORKS TOO (I HAD TROUBLE WITH THE ABOVE METHOD BEFORE)
    var _antigenLabel;
    var _rhfactor;
    this.setBloodType = setBloodType;

    function setBloodType(antigenLabel, rhfactor) {
        _antigenLabel = antigenLabel;
        _rhfactor = rhfactor;

    }
    Object.defineProperty(this, 'antigenLabel', {
        get: function () {
            return _antigenLabel
        },
        enumerable: true,
        configurable: false
    });
    Object.defineProperty(this, 'rhfactor', {
        get: function () {
            return _rhfactor
        },
        enumerable: true,
        configurable: false
   ...