Baseline Widely available
Array
ì¸ì¤í´ì¤ì push()
ë©ìëë ë°°ì´ì ëì ëª
ìë ìì를 ì¶ê°íê³ ë°°ì´ì ìë¡ì´ 길ì´ë¥¼ ë°íí©ëë¤.
const animals = ["pigs", "goats", "sheep"];
const count = animals.push("cows");
console.log(count);
// Expected output: 4
console.log(animals);
// Expected output: Array ["pigs", "goats", "sheep", "cows"]
animals.push("chickens", "cats", "dogs");
console.log(animals);
// Expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]
구문
push()
push(element1)
push(element1, element2)
push(element1, element2, /* â¦, */ elementN)
매ê°ë³ì
element1
, â¦, elementN
ë°°ì´ì ëì ì¶ê°í ìì(ë¤).
ì´ ë©ìëê° í¸ì¶ë ê°ì²´ì ìë¡ì´ length
ìì±.
push
ë©ìëë ë°°ì´ ëì ì¬ë¬ ê°ì ì¶ê°í©ëë¤.
Array.prototype.unshift()
ë push()
ì ëìê³¼ ë¹ì·íì§ë§ ë°°ì´ì ìì ë¶ë¶ì ì ì©ë©ëë¤.
push()
ë©ìëë ë³ì´ ë©ìëì
ëë¤. ì´ ë©ìëë this
ì 길ì´ì ë´ì©ì ë³ê²½í©ëë¤. this
ì ê°ì ëì¼íê² ì ì§íì§ë§ ëì ììê° ì¶ê°ë ìë¡ì´ ë°°ì´ì ë°íí길 ìíë¤ë©´ [arr.concat([element0, element1, /* ... ,*/ elementN])
]ì ëì ì¬ì©í ì ììµëë¤. ê·¸ë ì§ ìì¼ë©´ ìì ìì²´ê° ë°°ì´ì¸ ê²½ì° concat()
ì ëìì¼ë¡ ì¸í´ ììê° ë¨ì¼ ììë¡ í¸ìëë ëì ë¶ì°ëë¤ë ì ì ì ìíì기 ë°ëëë¤.
push()
ë ë²ì© ë©ìëì
ëë¤. ì¤ì§ this
ê°ì´ length
ìì±ê³¼ ì«ì í¤ ìì±ë§ ê°ì§ê³ ìì ê²ì 기ëí©ëë¤. 문ìì´ì´ ë°°ì´ê³¼ ì ì¬í긴 íì§ë§, 문ìì´ì ë¶ë³ì´ê¸° ë문ì ì´ ë©ìëë 문ìì´ìë ì´ì¸ë¦¬ì§ ììµëë¤.
ë¤ì ì½ëë 2ê°ì ìì를 ê°ì§ sports
ë°°ì´ì ìì±íê³ 2ê°ì ìì를 ì¶ê°í©ëë¤. total
ë³ìë ì´ ë°°ì´ì ìë¡ì´ 길ì´ë¥¼ ê°ì§ëë¤.
const sports = ["soccer", "baseball"];
const total = sports.push("football", "swimming");
console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total); // 4
ë ê°ì ë°°ì´ì í©ì¹ê¸°
ìë ìì ë spread syntax를 ì¬ì©íì¬ ë ë²ì§¸ ë°°ì´ì 모ë ìì를 첫 ë²ì§¸ì ë£ìµëë¤.
const vegetables = ["parsnip", "potato"];
const moreVegs = ["celery", "beetroot"];
// ë ë²ì§¸ ë°°ì´ì 첫 ë²ì§¸ ë°°ì´ë¡ í©ì¹©ëë¤
vegetables.push(...moreVegs);
console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']
concat()
ì ì¬ì©í´ìë ë ë°°ì´ì í©ì¹ ì ììµëë¤.
push()
ë©ìëë this
ì length
ìì±ì ì½ìµëë¤. ê·¸ë° ë¤ì, push()
ì ì ë¬ë ì¸ìë¤ì ì´ì©í´ this
ì length
ë¶í° ê° ì¸ë±ì¤ì ê°ì ì¤ì í©ëë¤. ë§ì§ë§ì¼ë¡, ì´ì length
ì ì¶ê°ë ììì ê°ì를 ëíì¬ length
를 ì¤ì í©ëë¤.
const arrayLike = {
length: 3,
unrelated: "foo",
2: 4,
};
Array.prototype.push.call(arrayLike, 1, 2);
console.log(arrayLike);
// { '2': 4, '3': 1, '4': 2, length: 5, unrelated: 'foo' }
const plainObj = {};
// length ìì±ì´ ìì¼ë¯ë¡ 길ì´ë 0ì
ëë¤.
Array.prototype.push.call(plainObj, 1, 2);
console.log(plainObj);
// { '0': 1, '1': 2, length: 2 }
ì ì¬ ë°°ì´ê³¼ ë°©ìì¼ë¡ ê°ì²´ ì¬ì©í기
ììì ì¸ê¸íë¯ì´ push
ë ìëì ì¼ë¡ ë²ì©ì´ë©°, ì´ë¥¼ ì 리íê² ì¬ì©í ì ììµëë¤. ì´ ìììì ë³¼ ì ìë¯ì´ Array.prototype.push
ë ê°ì²´ìì ì ìëí©ëë¤.
ê°ì²´ 컬ë ì
ì ì ì¥í기 ìí´ ë°°ì´ì ë§ë¤ì§ ìëë¤ë ì ì ì ìíì기 ë°ëëë¤. ëì ê°ì²´ ìì²´ì 컬ë ì
ì ì ì¥íê³ Array.prototype.push
ìì call
ì ì¬ì©íì¬ ë©ìëê° ë°°ì´ì ë¤ë£¨ê³ ìë¤ê³ ìê°íëë¡ ìì
ëë¤. ì´ë JavaScriptê° ìíë ë°©ìì¼ë¡ ì¤í ë§¥ë½ì ì¤ì í ì ìë ë°©ì ëë¶ì ìëí©ëë¤.
const obj = {
length: 0,
addElem(elem) {
// obj.lengthë ììê° ì¶ê° ë ëë§ë¤
// ìëì¼ë¡ ì¦ê°í©ëë¤.
[].push.call(this, elem);
},
};
// ì¤ëª
ì ìí´ ë¹ ê°ì²´ë¥¼ ëª ê° ì¶ê°í´ ë³´ê² ìµëë¤.
obj.addElem({});
obj.addElem({});
console.log(obj.length); // 2
obj
ê° ë°°ì´ì´ ìëìë, ì¤ì ë°°ì´ì ë¤ë£¨ë ê² ì²ë¼ push
ë©ìëê° ì±ê³µì ì¼ë¡ obj
ì length
ìì±ì ì¦ê°ìí¨ ê²ì 주목íì기 ë°ëëë¤.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4