简体   繁体   中英

javascript object.. Limit possible entries to two?

In class I need to do some javascripting. I've got an assignment to make a small script where I make a object. My problem now is that I need to limit the possible values of a property of my object. Here's My code:

var StudentObject = new Object();
var StudentStatus = new Object();
StudentStatus ={
   betaald:true,
   inschrijvingOK:true
}
StudentObject = {
   status: StudentStatus,
   traject: "",
   opleiding:"Toegepaste Informatica"
} 

In the object "StudentObject", you see a property "traject". This should only be able to contain "MBT" or "PBT". Is there an easy way?

Thanks in advance.

There is no way to restrict what values a public object property in javascript can contain as you have defined it.

Instead of the property itself, you could have accessor methods for getting and setting the value and those methods could enforce only certain values. You could then store the actual data inside a closure in a place not accessible to the outside world so only the accessors can get to it. See http://javascript.crockford.com/private.html for ways to implement private instance variables if you want to use accessors with private storage.

function StudentObject(studentStatus, opleiding) {
   // private instance variable, only available to code defined
   // in the constructor
   var traject = "";

   // public methods
   this.setTraject = function(val) {
       if (val === "MBT" || val === "PBT") {
           traject = val;
       }
   }

   this.getTraject = function() {
       return traject;
   }

   this.status = studentStatus;
   this.opleiding = opleiding;
}

var studentStatus = {
    betaald:true,
    inschrijvingOK:true
}

var student = new StudentObject(studentStatus, "Toegepaste Informatica");
student.setTraject("PBT");
alert(student.getTraject());    // "PBT"
student.setTraject("xxx");
alert(student.getTraject());    // "PBT"

您可能可以做到:

traject:function(x){(x=='MBT'||x=='PBT')?return x:return null;}

There is no easy way to do this :(

You might need to check set and new and Object.defineProperty

Code:

function StudentObject() {
    var _traject = undefined;

    Object.defineProperties(this, {
        traject: {
            enumerable: true,
            configurable: true,
            get: function () {
                return _traject;
            },
            set: function (value) {
                if ((typeof (value) === "string") && (value.indexOf("MBT") >= 0 || value.indexOf("PBT") >= 0)) {
                    _traject = value;
                }
            }
        }
    });
}


var s = new StudentObject();
s.traject = "foobar"; // won't set traject to foobar
console.log(s.traject); // undefined
s.traject = "someTextContainsMBT"; // will set traject
console.log(s.traject); // someTextContainsMBT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM