Skip to content

Instantly share code, notes, and snippets.

@tyaslab
Last active August 11, 2016 02:18
Show Gist options
  • Save tyaslab/619c09fc19159d239b077d20fee9a59e to your computer and use it in GitHub Desktop.
Save tyaslab/619c09fc19159d239b077d20fee9a59e to your computer and use it in GitHub Desktop.
javascript advance
function Mammal() {};
Mammal.prototype.sayHello = function() {
alert("Hello from mammal");
}
// inheritance step 1
function Cat() {
Mammal.call(this);
}
// inheritance step 2 (extends)
Cat.prototype = Object.create(Mammal.prototype);
// inheritance step 3 (constructor)
Cat.prototype.constructor = Cat;
Cat.prototype.sayHello = function() {
// polymorphism
Mammal.prototype.sayHello.call(this);
alert("Hello from cat");
}
var cat = new Cat();
cat.sayHello();
var promise = function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
document.getElementById("promise").innerHTML = "yay!";
resolve();
}, 2000);
});
}
promise().then(function() {
document.getElementById("wrapper").innerHTML = "okay done!";
}, function() {
});
document.getElementById("wrapper").innerHTML = "i will be disappeared!";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment