function user(first, last) {
this.firstName = first;
this.LastName = last;
}
user.prototype.getFullName = function() {
return `${this.firstName} ${this.LastName}`
}
const sungdae1 = new user('sungdae', 'jin');
const amy1 = new user('Amy', 'Clarke');
const neo1 = new user('Neo', 'Smith');
console.log(amy1.getFullName());
console.log(neo1.getFullName());
console.log(sungdae1.getFullName());
prototype 속성
과 객체 맴버인 proto
속성이 참조하는 숨은 링크가 있다.일반(Normal)함수는 호출 위치에 따라 this를 정의한다.
화살표(Arrow)함수는 자신이 선언된 함수 범위에서 this를 정의한다.
const sungdae2 = {
name: 'Sungdae',
normal() {
console.log(this.name);
},
arrow: () => {
console.log(this.name);
}
}
sungdae2.normal();
sungdae2.arrow();
const amy3 = {
name: 'Amy',
normal: sungdae2.normal,
arrow: sungdae2.arrow
}
amy3.normal();
amy3.arrow();
function User2(name) {
this.name = name;
}
User2.prototype.normal = function() {
console.log(this.name);
}
User2.prototype.arrow = () => {
console.log(this.name);
}
const sungdae4 = new User2('Sungdae');
sungdae4.normal();
sungdae4.arrow();
ex) 타임 함수
const timer = {
name: 'Sungdae!!',
timeout: function() {
setTimeout(() => {
console.log(this.name);
}, 2000)
}
}
timer.timeout();
class User {
constructor(first, last){
this.firstName = first;
this.lastName = last;
}
getFullName() {
return `${this.firstName} ${this.lastName}`
}
}
const sungdae = new User('sungdae', 'jin');
const amy = new User('Amy', 'Clarke');
const neo = new User('Neo', 'Smith');
console.log(sungdae);
console.log(amy.getFullName());
console.log(neo.getFullName());
class Vehicle{
constructor(name, wheel) {
this.name = name;
this.wheel = wheel;
}
}
const myVehicle = new Vehicle('운송수단', 2);
console.log(myVehicle);
class Bicycle extends Vehicle {
constructor(name, wheel) {
super(name, wheel);
}
}
const myBicycle = new Bicycle('삼천리', 2);
const daugtersBicycle = new Bicycle('세발', 3);
console.log(myBicycle);
console.log(daugtersBicycle);
class Car extends Vehicle {
constructor(name, wheel, license) {
super(name, wheel);
this.license = license;
}
}
const myCar = new Car('벤츠', 4, '1종 보통');
const yourCar = new Car('bmw', 4, '2종 보통');
console.log(myCar);
console.log(yourCar);