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 5 years have passed since last update.

J2complexedAdvent Calendar 2016

Day 4

ArrayやObjectを継承してみよう

Last updated at Posted at 2016-12-04

この記事は J2complexed Advent Calendar 2016 の4日目です。

#ES6ではclass構文を使ってArrayやObjectを簡単に継承できる

##Array

class ExArray extends Array {
  get_length() {
    return this.length;
  }
}

var array = new ExArray("hoge", "foo", "bar");
console.log(array); // ["hoge", "foo", "bar"]
console.log(array.get_length()); // 3
console.log(array.length); // 3
array.push("piyo");
console.log(array); // ["hoge", "foo", "bar", "piyo"]
console.log(array.get_length()); // 4
console.log(array.length); // 4

##Object

class ExObject extends Object {
  has_keys(key) {
    return this.hasOwnProperty(key);
  }
}

var object = new ExObject();
object["hoge"] = "foo";
console.log(object); // {"hoge": "foo"}
console.log(object.hasOwnProperty("hoge")); // true
console.log(object.has_keys("hoge")); // true
console.log(object.hasOwnProperty("foo")); // false
console.log(object.has_keys("foo")); // false

ちなみに、babelではサポートされてないので使えません:sweat_smile:

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?