先上code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>www.moyu-edu.com</title>
</head>
<body>
<script>
var next = function(){
console.log("so cool next");
}
var fn = function(next){
setTimeout(function(){
(typeof next == "function") && next();
})
}
fn();
</script>
</body>
</html>
我最喜欢的js特性就是promise,而且ES6也把promise加入了:
function helloWorld (ready) {
return new Promise(function (resolve, reject) {
if (ready) {
resolve("Hello World!");
} else {
reject("Good bye!");
}
});
}
helloWorld(true).then(function (message) {
alert(message);
}, function (error) {
alert(error);
});