我的脑海中总在浮现一个问题:“我能不能在写JavaScript的时候不出现if块?”
受Chris Owen对于SmallTalk的阐述启发我写出了类SmallTalk的无if实现。
Boolean.prototype.ifTrue = function (f) {
this && f();
return this;
};
Boolean.prototype.ifFalse = function (f) {
this || f();
return this;
};
// so you can write
(4 < 5).ifTrue(function () {
alert("It is true.");
}).ifFalse(function () {
alert("It isn’t true.");
});这个例子没有实际用途,但是在学习的角度来看是一个有趣的例子。
Copyright © 2019- ltcredit.com 版权所有
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务