var emptyObject = {};
console.log(typeof emptyObject); // object
var person = {
name: 'Lee',
gender: 'male',
sayHello: function(){
console.log('Hi! My name is ' + this.name);
}
};
console.log(person); // { name: 'Lee', gender: 'male', sayHello: [Function: sayHello] }
person.sayHello(); // Hi! My name is Lee
var person2 = new Object(); // new 연산자, Object 생성자 함수 사용해서 empty object 생성
//property 추가
person2.name = 'Yoon';
person2.gender = 'male';
person2.sayHello = function(){
console.log('Hi! My name is ' + this.name);
};
console.log(typeof person2); // object
console.log(person2); // { name: 'Yoon', gender: 'male', Hello: [Function] }
person2.sayHello(); // Hi! My name is Yoon
function Person(name, gender){
var married = true; // private
this.name = name; // public
this.gender = gender; //public
this.sayHello = function(){ //public
console.log('Hi! My name is ' + this.name);
};
}
var p1 = new Person('Lee', 'Male');
var p2 = new Person('Park', 'Female');
console.log('p1: ', typeof p1);
console.log('p2: ', typeof p2);
console.log('p1: ', p1);
console.log('p2: ', p2);
p1.sayHello();
p2.sayHello();
console.log(p1.married); // undefined
var person = {
'first-name': 'Ung-mo',
'last-name': 'Lee',
gender: 'male',
1: 10,
function: 1
};
var person = {
'first-name': 'Ung-mo',
'last-name': 'Lee',
gender: 'male',
1: 10
};
console.log(person.first-name); // NaN: undefined-undefined
console.log(person[first-name]); // ReferenceError: first is not defined
console.log(person['first-name']); // 'Ung-mo'
console.log(person.gender); // 'male'
console.log(person['gender']); // 'male'
console.log(person['1']); // 10
var person = {
'first-name': 'Ung-mo',
};
person['first-name'] = 'Kim';
console.log(person['first-name'] ); // 'Kim'
var person = {
'first-name': 'Ung-mo',
'last-name': 'Lee',
gender: 'male',
};
person.age = 20; // property 동적 생성
console.log(person.age); // 20
var person = {
'first-name': 'Ung-mo',
'last-name': 'Lee',
gender: 'male',
};
delete person.gender;
console.log(person.gender); // undefined
delete person;
console.log(person); // Object {first-name: 'Ung-mo', last-name: 'Lee'}
var array = ['one', 'two'];
array.name = 'my array';
for (var index in array) {
console.log(index + ': ' + array[index]);
}
/*
result
0: one
1: two
name: my array
*/
const array = [1, 2, 3];
array.name = 'my array';
for (const value of array) {
console.log(value);
}
/*
result
1
2
3
*/
for (const [index, value] of array.entries()) {
console.log(index, value);
}
/*
0 1
1 2
2 3
*/
const o1 = { a: 1 };
const o2 = { b: 2 };
const o3 = { c: 3 };
const merge1 = Object.assign(o1, o2, o3);
console.log(merge1); // { a: 1, b: 2, c: 3 }
console.log(o1); // { a: 1, b: 2, c: 3 }
const user1 = {
name: 'Lee',
address: {
city: 'Seoul'
}
};
const user2 = Object.assign({}, user1);
console.log(user1 === user2); // false
user2.name = 'Kim';
console.log(user1.name); // Lee
console.log(user2.name); // Kim
console.log(user1.address === user2.address); // true
const user1 = {
name: 'Lee',
address: {
city: 'Seoul'
}
};
Object.freeze(user1);
user1.name = 'Kim'; // 불변객체로 만들었으므로 무시
console.log(user1); // { name: 'Lee', address: { city: 'Seoul' } }
console.log(Object.isFrozen(user1)); // true
user.address.city = 'Busan';
console.log(user); // { name: 'Lee', address: { city: 'Busan' } }
[TestDome] HTML/CSS and JavaScript: Image Gallery (ECMAScript 7) (0) | 2021.01.31 |
---|---|
[TestDome] HTML/CSS and JavaScript: check digit (ECMAScript 7) (0) | 2021.01.31 |
[JavaScript] JavaScript #3: 연산자, 타입변환, 단축 평가 (0) | 2021.01.30 |
[JavaScript] JavaScript #2: 데이터 타입, 변수, 동적 타이핑(Dynamic Typing), 변수 호이스팅(Variable Hoisting) (0) | 2021.01.29 |
[JavaScript] JavaScript #1: 자바스크립트 개요 (0) | 2021.01.29 |