constructorFunction.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let user1 = {
name: "green",
age: 20,
print: function() {
console.log("hi")
}
}
console.log(user1.name)
user1.print();
// 생성자 함수
function User(name, age) {
// this = {};
this.name = name;
this.age = age;
this.print = () => {
console.log(`내 이름은 ${this.name}입니다.`)
}
// return this;
}
let user2 = new User("blue", 33)
user2.print();
</script>
</body>
</html>
'Stack > JavaScript' 카테고리의 다른 글
[JS] fusionChart (1) | 2022.09.01 |
---|---|
[JS] 2022 데브매칭 상반기 - 프로그래밍 언어 검색 (0) | 2022.08.22 |
[JS] Promise / Async / 에러 핸들링 복습 (0) | 2022.06.30 |
[JS] Wave (0) | 2022.06.20 |
[JS] JumpGame (0) | 2022.06.17 |