Home > Web Front-end > JS Tutorial > body text

Eliminate if in JavaScript

高洛峰
Release: 2016-11-28 11:28:47
Original
1374 people have browsed it

Email:longsu2010 at yeah dot net


A question always pops up in my mind: "Can I write JavaScript without if blocks?"

Inspired by Chris Owen's explanation of SmallTalk, I wrote An if-less implementation of class SmallTalk.


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.");
});This example has no practical use, but it is an interesting example from a learning perspective.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!