Javascript/객체

Object

appmaster 2021. 1. 26. 15:06

함수, 클래스 (틀) => 객체, 개체, object

 

 

생성자 함수로 객체 만들기

function 틀() {} => new 틀()
function B(name, age){
    console.log(name, age);
}

const b = new B();
const c = new B('nana', 21);
console.log(B());
//출력
undefined undefined
nana 21
undefined undefined
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');
    }
}

new B().hello();
//출력
hello