var baskikball = {
name: 'ik',
nickname: 'durant'
};
//
console.log(foo.nickname); // durant
delete foo.nickname;
console.log(foo.nickname); // undefined
console.log(foo); // {name: "ik"}
[]와 {}의 차이.
```javascript
var bkn = [];
var durant = {};
bkn.constructor.name; // "array"
durant.constructor.name; // "object"
var arr = [1,2,3];
delete arr[1];
console.log(arr); // [1, undefined × 1, 3]
var arr = [1, 2, 3];
arr.splice(1, 1);
console.log(arr); // [1, 3]
console.log(1 == '1'); // true
console.log(1 === '1'); // false
add(2, 3); // add is not a function
var add = function (a, b) {
return a + b;
};
add(4, 5);
// 첫번째 줄의 실행결과는 add is not a function이다. javascript hoisting을 적용하여 코드 순서를 변경하면 아래처럼 실행이 된다.
var add;
add(2, 3);
add = function (a, b) {
return a + b;
};
add(4, 5);
함수의 내부에 정의한 함수
function parent() {
var a = 10;
var b = 20;
function child() {
var b = 30;
console.log(a);
console.log(b);
}
child();
}
parent(); // 10, 30
child(); // child is not defined
object instanceof constructor;
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
var mycar = new Car("Honda", "Accord", 1998);
var a = mycar instanceof Car; // returns true
var b = mycar instanceof Object; // returns true
function Func(arg) {
// instanceof 로 생성자 함수임을 확인
if (!(this instanceof arguments.callee)) // 'this instanceof 함수명' 도 가능
return new Func(arg);
this.value = arg || 0;
}
var a = new Func(100);
var b = Func(200);
console.log(a.value);
console.log(b.value);
function func() {
return true;
}
console.log(func.prototype); // {constructor: f}
console.log(func.prototype.constructor); // f func(){return true;};
var obj = {
name: 'captain',
printName: function () {
console.log(this.name);
}
};
obj.printName(); // 'captain'
obj.hasOwnProperty('name'); // true
obj.hasOwnProperty('city'); // false
obj에서 사용한 printName()메서드는 obj에 선언이 되었기에 사용할 수 있다. 하지만 hasOwnProperty() 메서드는 선언되지도 않았는데 사용할 수 있다. 그 이유는 obj의 프로토타입 객체가 Object이고, Object에 내장된 메서드가 hasOwnProperty()이기 때문에, obj에서 프로토타입 객체의 hasOwnProperty()를 호출한다.
String.prototype.printText = function (text) {
console.log("Print this text out " + text);
};
var name = "captain";
name.printText('pangyo'); // 'Print this text out pangyo'
(function (name) {
console.log('This is the immediate function : ' + name);
})('foo');
function parent() {
var a = 'Parent is done';
function child() {
console.log(a);
}
return child;
}
var closure = parent();
closure();
캡틴판교님의 블로그를 참고했습니다.