함수, 클래스 (틀) => 객체, 개체, object
function 틀( ) { } => new 틀( ) : 생성자 함수로 객체 만들기
function A() {}
const a =new A();
console.log(a, typeOf a); // A {} 'object' 출력됨
console.log(A()); // undefined 출력됨
//생성하면서 데이터 넣기
function B(name, age) {
console.log(name, age); // Mark 37 출력됨
}
const b = new B();
const c = new B('Mark', 37);
console.log(B()); // undefined 출력됨
객체에 속성 추가하기 property
//값을 속성으로 넣기
function A() {
this.name = 'Mark';
}
const a = new A();
console.log(a); // A { name : 'Mark' } 출력됨
//함수를 속성으로 넣기
function B() {
this.hello = function() {
console.log('hello'); // hello 출력됨
}
}
new B().hello();
프로토타입 체인 .prototype
function Percon(name, age) {
this.name = name;
this.age = age;
this.hello = function() {
console.log('hello', this.name, this.age); // hello Mark 37 출력됨
};
}
const p =new Person('Mark', 37);
p.hello();
console.log(p.toString()); // [object Object] 출력됨
console.log(Person.prototype); // Person{} 출력됨
console.log(Person.prototype.toString); // [Function: toString] 출력됨
console.log(Person.prototype.constructor); // [Function: Person] 출력됨
console.log(Person.hello); // undefined 출력됨
console.log(Person.prototype.hello); // undefined 출력됨
표준 내장 객체
: 객체가 이미 런타임 환겨에 만들어져있는 것
: 대표적으로 object, new function ...
const a = new Array('red', 'black', 'white');
console.log(a, typeOf a); // ['red', 'black', 'white'] 'object' 출력됨
console.log(a, instanceof Array); // true 출력됨
console.log(a, instanceof Object); // true 출력됨
const b = ['red', 'green', 'yello'];
console.log(b, typeOf b); // ['red', 'green', 'yello'] 출력됨
console.log(b, instanceof Array); // true 출력됨
console.log(b, instanceof Object); // true 출력됨
console.log(b.slice(0, 1)); // ['red'] 출력됨
console.log(Array.prototype.slice, Object.prototype.slice); // [function: slice] undefine 출력됨
'Javascript' 카테고리의 다른 글
[JavaScript] 함수 (0) | 2022.04.05 |
---|---|
[JavaScript] 반복문 (0) | 2022.04.04 |
[JavaScript] 조건문 (0) | 2022.04.04 |
[JavaScript] 시작하면서 (0) | 2022.03.29 |