반응형
리터럴 객체 선언
let func = (item1, item2) => item1 + item2;
console.log(func(1,2));
const emptyObject = {};
emptyObject.name = "LEee";
console.log(emptyObject.name);
const user = {
age:20,
name:"디디에 드록바",
get_data: () => (1+2),
};
console.log(typeof user, user);
console.log(user.age);
console.log(user.name);
console.log(user.get_data());
const Lee = {
age: 25,
name : "Lee",
details:{
hobby: "coding",
major: "software",
get_detalis:function(item){
return this.major;
}
},
get get_age(){
return this.age;
},
set set_age(value){
this.age = value;
}
}
console.log(typeof Lee, Lee);
console.log(Lee.age);
console.log(Lee.details.hobby);
console.log(Lee.details.get_detalis(4));
console.log(Lee.get_age);
Lee.set_age = 24;
console.log(Lee.get_age);
const a = new Object();
a.age = 10;
a.name = "Daa";
console.log(a.age, a.name);
function User(age, name){
this.age = age;
this.name = name;
}
const Kim = new User(3,"Kim");
console.log(typeof Kim);
console.log(Kim.age, Kim.name);
User.prototype.message = function(){
return "hellow";
}
console.log(Kim.message());
클래스 객체 선언
class Animal{
constructor(name, age){
this.name = name;
this.age = age;
}
}
class Dog extends Animal{
constructor(name, age){
super(name,age);
}
bark = function(){
return "war war!";
};
}
const shunider = new Dog("bbo", 12);
console.log(shunider.name);
console.log(shunider.age);
console.log(shunider.bark());
bbo
abc.js:23 12
abc.js:24 war war!
prototype과 hasOwnProperty
class Animal{
constructor(name){
this.name = name;
}
get_message(){
return "Hello";
}
}
Animal.prototype.age = 10;
const dog = new Animal("dog");
console.log(dog.hasOwnProperty('name')); //true
console.log(dog.hasOwnProperty('age')); //false
반응형
'Web > javascript' 카테고리의 다른 글
appendChild를 이용하여 drag Event 만들기 (0) | 2022.08.10 |
---|---|
event.stopPropagation() 는 이벤트 버블링을 막는다. (0) | 2022.07.16 |