1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Javascript】IE上でスプレッド構文の代用としてPolyfill+Object.assignを用いて実装。

Posted at

メモとして残します。

トランスパイルをしない場合でスプレッド構文と同じような実装のやりかたを記述します。

(JSはレガシーな環境だと目的達成が大変です・・・)
#■やり方
###1. headでPolyfillを読む
(オンライン限定)

<script
  src="https://polyfill.io/v3/polyfill.min.js"
  crossorigin="anonymous">
</script>

###2. 下記のようにObject.assignがっちゃんこ。

let obj1 = {
    'hoge':'hogehoge',
    'hige':'higehige'
}
let obj2 =  Object.assign(
    obj1,
    {
        'hige':'HIGEHIGE',
        'fuge':'fugefuge'
    }
)
// obj2 → {hoge: "hogehoge", hige: "HIGEHIGE", fuge: "fugefuge"}

//スプレッド構文の場合
//let obj1 = {
//    'hoge':'hogehoge',
//    'hige':'higehige'
//}
//let obj2 = {
//    ...obj1,
//    'hige':'HIGEHIGE',
//    'fuge':'fugefuge'
//    
//}

ここで一つ注意しないといけないのは、
プロトタイプチェーンされているオブジェクトの場合は上記のような動作にならないため注意が必要。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?