Skip to content

Instantly share code, notes, and snippets.

@sedera-tax
Last active November 5, 2020 12:20
Show Gist options
  • Select an option

  • Save sedera-tax/174e1802e5eff3f701fa271e4d1b5ccc to your computer and use it in GitHub Desktop.

Select an option

Save sedera-tax/174e1802e5eff3f701fa271e4d1b5ccc to your computer and use it in GitHub Desktop.
Tuto ES6
/*
CLASS
*/
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
get area() {
return this.calcArea();
}
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(5, 5);
const poster = new Rectangle(2, 3);
console.log(square.height); //5
console.log(poster.width); //3
console.log(square.area); // 25
class Point {
constructor(a, b) {
this.x = a;
this.y = b;
}
getX() {
return this.x;
}
getY() {
return this.y;
}
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.hypot(dx, dy);
}
}
const p1 = new Point(7, 2);
const p2 = new Point(3, 8);
console.log(Point.distance(p1, p2));
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + ' makes a noise.');
}
}
class Dog extends Animal {
speak() {
console.log(this.name + ' barks.');
}
}
let dog = new Dog('Rex');
dog.speak();
class Human {
constructor(name) {
this.name = name;
}
hello() {
console.log('My name is ' + this.name);
}
}
class Student extends Human {
constructor(name, age) {
super(name);
this.age = age;
}
hello() {
super.hello();
console.log('I\'m ' + this.age + ' years old');
}
}
let student = new Student('John', 25);
student.hello();
let tree = {
height: 10,
color: 'green',
grow() {
this.height += 2;
}
};
tree.grow();
console.log(tree.height); // 12
let height = 5;
let health = 100;
let athlete = {
height,
health
};
console.log(athlete);
let arr = ['1', '2', '3'];
let [one, two, three] = arr;
console.log(one); // 1
console.log(two); // 2
console.log(three); // 3
let a = () => {
return [1, 3, 2];
};
let [o, , t] = a();
console.log(o); // 1
console.log(t); // 2
//Array Finding
var res = [4, 5, 1, 8, 2, 0].filter(function (x) {
return x > 4;
})[0];
console.log(res);
//find
var res = [4, 5, 1, 8, 2, 0].find(x => x > 4);
console.log(res);
//findIndex
var index = [4, 5, 1, 8, 2, 0].findIndex(x => x > 3);
console.log(index);
//Reapeting strings
console.log(Array(3 + 1).join("foo")); // foofoofoo
console.log("foo".repeat(3)); // foofoofoo
//Searching strings
"SoloLearn".startsWith("Solo", 0); // true
"SoloLearn".endsWith("Solo", 4); // true
"SoloLearn".includes("loLe"); // true
"SoloLearn".includes("olo", 1); // true
"SoloLearn".includes("olo", 2); // false
/*
Promise
*/
function asyncFunc(work) {
return new Promise(function(resolve, reject) {
if (work === "")
reject(Error("Nothing"));
setTimeout(function() {
resolve(work);
}, 1000);
});
}
asyncFunc("Work 1") // Task 1
.then(function(result) {
console.log(result);
return asyncFunc("Work 2"); // Task 2
}, function(error) {
console.log(error);
})
.then(function(result) {
console.log(result);
}, function(error) {
console.log(error);
});
console.log("End");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment