Last active
March 21, 2022 04:11
-
-
Save jhades/0ff91346682e257b91550518b0c60a5d to your computer and use it in GitHub Desktop.
Javascript for Java Developers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create an empty object - no class was needed !! | |
var superhero = {}; | |
superhero.name = 'Superman'; | |
superhero.strength = 100; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<String,Object> superhero = new HashMap<>(); | |
superhero.put("name","Superman"); | |
superhero.put("strength", 100); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var flyFunction = function() { | |
console.log('Flying like a bird!'); | |
}; | |
superhero.fly = flyFunction; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// prints 'Flying like a bird!' to the console | |
superhero.fly(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Power { | |
void use(); | |
} | |
public class SuperHero { | |
private Power flyPower; | |
public void setFly(Power flyPower) { | |
this.flyPower = flyPower; | |
} | |
public void fly() { | |
flyPower.use(); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java 7 equivalent | |
Power flyFunction = new Power() { | |
@Override | |
public void use() { | |
System.out.println("Flying like a bird ..."); | |
} | |
}; | |
// Java 8 equivalent | |
superman.setFly( | |
()->System.out.println("Flying like a bird ...")); | |
superman.fly(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var superman = { | |
heroName: 'Superman', | |
sayHello: function() { | |
console.log("Hello, I'm " + this.heroName ); | |
} | |
}; | |
superman.sayHello(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var failThis = superman.sayHello; | |
failThis(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// overrides 'this' with superman | |
hello.call(superman); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var avengersHero = { | |
editor: 'Marvel' | |
}; | |
var ironMan = {}; | |
ironMan.__proto__ = avengersHero; | |
console.log('Iron Man is copyrighted by ' + ironMan.editor); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function SuperHero(name, strength) { | |
this.name = name; | |
this.strength = strength; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
avengersHero.editor = 'DC Comics'; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var superman = new SuperHero('Superman', 100); | |
console.log('Hello, my name is ' + superman.name); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function SuperHero(name, strength) { | |
this.name = name; | |
this.strength = strength; | |
} | |
SuperHero.prototype.sayHello = function() { | |
console.log('Hello, my name is ' + this.name); | |
} | |
var superman = new SuperHero('Superman', 100); | |
superman.sayHello(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var superHeroPrototype = { | |
sayHello: function() { | |
console.log('Hello, my name is ' + this.name); | |
} | |
}; | |
var superman = Object.create(superHeroPrototype); | |
superman.name = 'Superman'; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface FlyCommand { | |
public void fly(); | |
} | |
public class FlyingHero { | |
private String name; | |
public FlyingHero(String name) { | |
this.name = name; | |
} | |
public void fly(FlyCommand flyCommand) { | |
flyCommand.fly(); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String destination = "Mars"; | |
superMan.fly(() -> System.out.println("Flying to " + | |
destination )); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var destination = 'Mars'; | |
var fly = function() { | |
console.log('Fly to ' + destination); | |
} | |
fly(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createHero(heroName) { | |
var name = heroName; | |
return { | |
fly: function(destination) { | |
console.log(name + ' flying to ' + destination); | |
} | |
}; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var superman = createHero('SuperMan'); | |
superman.fly('The Moon'); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function counterLoop() { | |
console.log('counter before declaration = ' + i); | |
for (var i = 0; i < 3 ; i++) { | |
console.log('counter = ' + i); | |
} | |
console.log('counter after loop = ' + i); | |
} | |
counterLoop(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function counterLoop() { | |
var i; // i is 'seen' as if declared here! | |
console.log('counter before declaration = ' + i); | |
for (i = 0; i < 3 ; i++) { | |
console.log('counter = ' + i); | |
} | |
console.log('counter after loop: ' + i); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function counterLoop() { | |
var i; // i is 'seen' as if declared here! | |
console.log('counter before declaration = ' + i); | |
for (i = 0; i < 3 ; i++) { | |
console.log('counter = ' + i); | |
} | |
console.log('counter after loop: ' + i); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In 09-override-this.js
hello.call(superman);
should be
failThis.call(superman);