Skip to content

Instantly share code, notes, and snippets.

@jhades
Last active March 21, 2022 04:11
Show Gist options
  • Save jhades/0ff91346682e257b91550518b0c60a5d to your computer and use it in GitHub Desktop.
Save jhades/0ff91346682e257b91550518b0c60a5d to your computer and use it in GitHub Desktop.
Javascript for Java Developers
// create an empty object - no class was needed !!
var superhero = {};
superhero.name = 'Superman';
superhero.strength = 100;
Map<String,Object> superhero = new HashMap<>();
superhero.put("name","Superman");
superhero.put("strength", 100);
var flyFunction = function() {
console.log('Flying like a bird!');
};
superhero.fly = flyFunction;
// prints 'Flying like a bird!' to the console
superhero.fly();
public interface Power {
void use();
}
public class SuperHero {
private Power flyPower;
public void setFly(Power flyPower) {
this.flyPower = flyPower;
}
public void fly() {
flyPower.use();
}
}
// 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();
var superman = {
heroName: 'Superman',
sayHello: function() {
console.log("Hello, I'm " + this.heroName );
}
};
superman.sayHello();
var failThis = superman.sayHello;
failThis();
// overrides 'this' with superman
hello.call(superman);
var avengersHero = {
editor: 'Marvel'
};
var ironMan = {};
ironMan.__proto__ = avengersHero;
console.log('Iron Man is copyrighted by ' + ironMan.editor);
function SuperHero(name, strength) {
this.name = name;
this.strength = strength;
}
avengersHero.editor = 'DC Comics';
var superman = new SuperHero('Superman', 100);
console.log('Hello, my name is ' + superman.name);
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();
var superHeroPrototype = {
sayHello: function() {
console.log('Hello, my name is ' + this.name);
}
};
var superman = Object.create(superHeroPrototype);
superman.name = 'Superman';
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();
}
}
String destination = "Mars";
superMan.fly(() -> System.out.println("Flying to " +
destination ));
var destination = 'Mars';
var fly = function() {
console.log('Fly to ' + destination);
}
fly();
function createHero(heroName) {
var name = heroName;
return {
fly: function(destination) {
console.log(name + ' flying to ' + destination);
}
};
}
var superman = createHero('SuperMan');
superman.fly('The Moon');
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();
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);
}
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);
}
@Craciunvd
Copy link

In 09-override-this.js

hello.call(superman);

should be

failThis.call(superman);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment