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?

JavaScriptのArrayのprototype拡張

Posted at

prototype拡張とは

JavaScriptのprototype拡張では、組み込みのオブジェクトにメソッドを新規追加できます。
ここでは、Arrayのprototype拡張を紹介します。

実例

定義

// 「XXX.prototype.methodName = function(){操作}」の形式でprototype拡張する
Array.prototype.excludeNullish = function(){
    // this(メソッドを呼び出すArray型のオブジェクト)のfilterメソッドを呼び出す
    // Array内部の値から、nullではなく、かつundefined型でもない値のみ残す
    return this.filter(v => v !== null && (typeof v) !== "undefined")
}

実行

[1,2,null,3,undefined].excludeNullish()

表示結果

(3)[1, 2, 3]
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?