[JavaScript] 기존 Object 에 변수나 함수 추가하기

in #javascript6 years ago

아래와 같은 objectA 가 있다고 하자.
var objectA = {
 arrayA = [],
 functionA : function() {
  return "A";
 }
}

objectA 에 arrayB 라는 변수를 추가하려면 다음과 같이 하면 된다.
objectA.arrayB = [];

arrayB가 있다면 추가하지 않고, 없는 경우에만 추가하고싶다면,
if(objectA.arrayB == null) {
 objectA.arrayB = [];
}

위와 같이 감싸면 된다.

함수도 똑같다.
objectA.functionA = function() { return "B" };

위와 같이 하면 funtionA "A"가 아닌 "B"를 돌려준다.

Sort:  

Congratulations @navi-kim! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

3 years on Steem - The distribution of commemorative badges has begun!
Happy Birthday! The Steem blockchain is running for 3 years.
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.032
BTC 60180.31
ETH 3001.45
USDT 1.00
SBD 3.64