function addPrivateProperty(o,name,predicate){
var value;
o["get"+name]=function(){return value;};
o["set"+name]=function(v){
if (predicate && !predicate(v))
throw Error("set"+name+": invalid value "+v);
else
value=v;
};
}
var o={};
addPrivateProperty(o,"Name",function(x){return typeof x=="string";});
here,what does "if (predicate && !predicate(v))" return? when would it return true/false?
如果未定义则为False,否则为true
I haven't done javascript in a while so you'll have to bear with me but if I remember correctly, all functions are just variables, so saying if(predicate)
just means if predicate was defined. In this scope it means if a value (function) was supplied to the parameter.
So essentially if (predicate && !predicate(v))
first checks to see if the function exists before calling it, because they do not want to call a function that doesnt exist
The statement if (predicate && !predicate(v))
evaluates if predicate and the opposite of predicate(v) are both true
.
This would be true for predicate
or !predicate(v)
being equal to anything other than null
, false
, undefined
, 0
, NaN
, or an empty string.
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.