不,就像新月说的那样。在下面,您可以找到一个示例,该示例说明如何在不使用eval的情况下但使用内部私有对象进行实施。
var test = function () { var prv={ }; function prop(name, def) { prv[name] = def; return function(value) { // if (!value) is true for 'undefined', 'null', '0', NaN, '' (empty string) and false. // I assume you wanted undefined. If you also want null add: || value===null // Another way is to check arguments.length to get how many parameters was // given to this function when it was called. if (typeof value === "undefined"){ //check if hasOwnProperty so you don't unexpected results from //the objects prototype. return Object.prototype.hasOwnProperty.call(prv,name) ? prv[name] : undefined; } prv[name]=value; return this; } }; return pub = { a:prop('a', 1), b:prop('b', 2), c:prop('c', 3), d:function(){ //to show that they are accessible via two methods //This is a case where 'with' could be used since it only reads from the object. return [prv.a,prv.b,prv.c]; } };}();


