Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: Cannot modify header information - headers already sent by (output started at /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php:340) in /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/all-in-one-seo-pack/app/Common/Meta/Robots.php on line 87

Warning: Cannot modify header information - headers already sent by (output started at /home/youjipdkmmwx/web-tweets.com/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php:340) in /home/youjipdkmmwx/web-tweets.com/public_html/wp-includes/feed-rss2.php on line 8
javascript | 知ってほしいWebのあれこれ https://web-tweets.com webに関する様々な情報をお伝えします。 Wed, 25 May 2022 06:40:21 +0000 ja hourly 1 getElementsByTagName() 要素取得方法と2つの注意点 https://web-tweets.com/javascript/elementsbytagname/ Wed, 25 May 2022 06:40:21 +0000 https://web-tweets.com/?p=447 タグの要素名からタグ情報を取得したいときに使うのがgetElementsByTagNameです。構文を使って見る前に注意事項や代替方法をしっかり理解するのが重要です。今回はgetElementsByTagNameの要素取...

The post getElementsByTagName() 要素取得方法と2つの注意点 first appeared on 知ってほしいWebのあれこれ.]]>
タグの要素名からタグ情報を取得したいときに使うのがgetElementsByTagNameです。構文を使って見る前に注意事項や代替方法をしっかり理解するのが重要です。今回はgetElementsByTagNameの要素取得方法や注意点2つを説明します。

getElementsByTagNameの要素取得方法

構文

var elements = document.getElementsByTagName(name);
上記はelementsの変数に、getElementsByTagNameで取得した要素を格納しています。

nameに取得(検索)したい要素を指定します。例えばh1タグを検索する場合はこうなります。

var elements = document.getElementsByTagName(“h1”);

特定の子要素の中でgetElementsByTagNameを指定したい場合

HTML文書のタグを全部取得するなら、そのままgetElementsByTagNameでいいかと思いますが、特定の子要素内のタグを検索したい場合は、特定の子要素を指定してからgetElementsByTagNameを指定すると良いでしょう。

以下は、specialのclass IDを持つ特定の子要素を指定した後に、<p>タグの要素をgetElementsByTagNameで取得しているコードです。

<body>
  <p>これは取得したくない</p>
  <p>これは取得したくない</p>

  <div id="special>
    <p>取得したい要素</p>
    <p>取得したい要素</p>
    <p>取得したい要素</p>
  </div>
</body>
<script>
      var special = document.getElementById('special');
      var specialParas = special.getElementsByTagName('p');
</script>

上記は「<p>これは取得したくない</p>」を避けて、specialの子要素内の<p>タグを取得しています。

getSelectorAllでも似たように要素を取得できる

タグの要素名の取得は他のメソッドでも可能です。例えばgetSelectorAllは、似たように複数の要素名を取得することが可能です。

以下はページ内の全てのh1タグを取得する構文です。

var elements = document.getSelectorAll(“h1”);

getSelectorAllの注意点

getSelectorAllでは、classを取得する場合は(.ドット),IDを取得する場合は(#)を前につけるなどルールは異なりますので、事前に確認しておきましょう。

getElementsByTagNameの注意点

変更した要素は更新される

getElementsByTagNameはHTMLコレクションである(生きているともいう)ので、元のDOMが変更された場合、取得する要素は変更されます。(更新されたら自動で同期される)

例えば、getElementsByTagNameで、要素をjavascriptで削除した場合は、削除下状態の要素が取得できます。

getSelectorは同期されない

getElementsByTagNameは取得要素が変更された場合は同期されますが、同じ用途でよく使われるgetSelectorは同期されません。変更前の情報を取得したい場合はgetSelectorを使うと良いでしょう。

取得要素が1つでもリストを指定する

getElementsByTagNameはNodeListを返します。そのため、取得した要素が1つであっても、要素の取得はarrayのデータ取得のように、[0]の指定が必要です。

document.getElementsByTagName("h1")[0];

上記は取得したh1の中で1番最初の要素を返します。

まとめ

getElementsByTagNmeについて説明しました。間違いやすい注意点や構文をしっかり押さえておきましょう。教科書的な情報は以下を参考にしてみると良いでしょう。

The post getElementsByTagName() 要素取得方法と2つの注意点 first appeared on 知ってほしいWebのあれこれ.]]>
canvasのarc()メソッド6つの引数と使うときの注意点 https://web-tweets.com/javascript/arc-method/ Thu, 06 Jan 2022 14:36:17 +0000 https://web-tweets.com/?p=420 グラフィックアニメーションなどの表現が広がるcanvasのarc()メソッドについて、調べたので備忘録ととして皆さんの勉強に役立てばいいと思って参考に共有しています。 円の描画をarc()メソッドで行う arc()メソッ...

The post canvasのarc()メソッド6つの引数と使うときの注意点 first appeared on 知ってほしいWebのあれこれ.]]>
グラフィックアニメーションなどの表現が広がるcanvasのarc()メソッドについて、調べたので備忘録ととして皆さんの勉強に役立てばいいと思って参考に共有しています。

円の描画をarc()メソッドで行う

arc()メソッドとは2次元の描写で円を描く際に使います。またarc()メソッドで円を描写するためには、以下の引数6つを指定します。

arc()メソッドで使う6つの引数
  1. 描写する円の中心位置:x座標
  2. 描写する円の中心位置:y座標
  3. 円の半径
  4. 起点の角度
  5. 終点の角度
  6. 描画方向(時計回りor反時計回り)

arc()メソッドのコード例と実描画

arc()メソッドのコード例

<canvas id="circle" height="320" width="320" style="background-color:gray;">円を描写</canvas>

<script>
    const canvas = document.getElementById("circle");
    const ctx = canvas.getContext("2d");
    ctx.arc(100,200,100,Math.PI*2,Math.PI,false);
    ctx.fill();
</script>

arc()メソッドで書いたコード例を実際に表示してみる

円を描写


コードの解説

  • canvasで指定した320×320ピクセル(width,height)のキャンバスを用意
  • arcメソッドの第一引数で、キャンバス内のx座標位置100ピクセルを円の中心位置に指示
  • arcメソッドの第二引数で、キャンバス内のy座標位置200ピクセルを円の中心位置に指示
  • arcメソッドの第三引数で、円の半径100ピクセルを指示
  • arcメソッドの第四引数で、描写起点の角度を指示
  • arcメソッドの第五引数で、描写終点の角度を指示
  • arcメソッドの第六引数で、時計回り(false)を指示

使う時の注意点

  • arc()メソッドともにfill()メソッド(もしくはstoroke()メソッド)を合わせて使うことで円の描画が可能
  • 円の連続描写は、beginPath()メソッドで都度初期化が必要
  • 直線描写でサブパスが使われた場合でも、beginPath()メソッドで初期化を行う

 

The post canvasのarc()メソッド6つの引数と使うときの注意点 first appeared on 知ってほしいWebのあれこれ.]]>
JSのコンストラクター関数のprototype(プロトタイプ)とは?どんな時に使ったらいいか。 https://web-tweets.com/javascript/prototype/ Sun, 28 Nov 2021 07:48:52 +0000 https://web-tweets.com/?p=409 なんでコンストラクター関数のprototypeを使うのか、どんな時に使ったらいいのか忘れがちになりそうなので、JSのコンストラクター関数時に使うprototypeのメモを公開します。   prototype(プ...

The post JSのコンストラクター関数のprototype(プロトタイプ)とは?どんな時に使ったらいいか。 first appeared on 知ってほしいWebのあれこれ.]]>
なんでコンストラクター関数のprototypeを使うのか、どんな時に使ったらいいのか忘れがちになりそうなので、JSのコンストラクター関数時に使うprototypeのメモを公開します。

 

prototype(プロトタイプ)とは

prototype(プロトタイプ)とは、「オブジェクトに存在する特別なプロパティ」で、「コンストラクター関数と共に使う」ものです。

例えばこんなコンストラクター関数があります。

function Person(name, age) {
    this.name = name;
    this.age = age;
}

const john = new Person('John', 37);
const tom = new Person('Tom', 24);

このコンストラクター関数にプロトタイプのプロパティを使うためには、コンストラクター関数「Person」に「prototype」を付けてアクセスできます。(この「prototype」は特別なプロパティです。)そして、このプロトタイプにメソッドを追加することができます。

 

function Person(name, age) {
    this.name = name;
    this.age = age;
}

//以下がprototypeを用いてメソッドを追加
Person.prototype.hello = function(){
    console.log('hello ' + this.name);
}

const john= new Person('John', 37);
const tom = new Person('Tom', 24);

上記のようにprototypeを用いることでインスタンス化したオブジェクト(johnやtom)にhelloというメソッドを渡すことができます。

以下の様に、すなわち「john」のオブジェクトにhelloメソッドを実行する「john.hello()」と「hello John」を表示することができます。

function Person(name, age) {
    this.name = name;
    this.age = age;
}

Person.prototype.hello = function(){
    console.log('hello ' + this.name);
}

const john = new Person('John', 37);
const tom = new Person('Tom', 24);
// johnのオブジェクトでhelloメソッドを実行する
john.hello();
// 実行結果は「hello John」となる

prototype(プロトタイプ)の仕組み

  • prototypeオブジェクトにメソッドを追加すると、「__proto__」に参照がコピーされる
  • この参照をたどることで各インスタンス(johnやtom)で関数(hello)を読み出す仕組みになっている
  • 「__proto__」はprototypeの参照なので、「john.__proto__ ===  tom.__proto__」はtrue(一緒)になる

ちなみにコンストラクター関数内にメソッドをいれるのと、プロトタイプにすることの違いとは?

ふと思ったのだが、コンストラクター関数にメソッドを入れてはいけないかと思った訳だが、結論から言うとプロトタイプの方が処理の面からよさそうということが分かった。例えば、以下の様にコンストラクター関数にメソッドを入れることもできる。

function Person(name, age) {
    this.name = name;
    this.age = age;
    // コンストラクター関数にhelloメソッドを入れても結果は同じ
    this.hello = function(){
        console.log('hello '+ this.name);
    }
}

const john = new Person('John', 37);
const tom = new Person('Tom', 24);

john.hello();

これでも実行結果は一緒なので、何ら問題はない。

ではなんでプロトタイプを使うのか?というと、プロトタイプとプロトタイプではない場合でメモリ処理に違いがあるからです。

プロトタイプは参照だからメモリの効率化ができる。

プロトタイプはあくまで参照なので、格納されているプロパティは同じ関数を参照しています。一方、プロトタイプを使わない処理は、インスタンスを生成する度(john.hello()やtom.hello()を生成する度)にhelloという関数を追加する必要があるため、プロトタイプに比べてメモリを多く消費することになります。

つまり、効率的にプログラムを動かす場合は、プロトタイプの方が良いという話でした。

おさらいですが、コンストラクター関数からインスタンス化した時には、prototypeの参照が「__proto__」がコピーされます。都度処理を促すコードなのではなく、効率的なメモリ処理にするため、プロトタイプを上手く使いましょう。

The post JSのコンストラクター関数のprototype(プロトタイプ)とは?どんな時に使ったらいいか。 first appeared on 知ってほしいWebのあれこれ.]]>
JavaScriptの非同期で理解が必要なマクロタスクとマイクロタスクの実行順番と関数 https://web-tweets.com/javascript/microtask-macrotasks/ Sat, 20 Nov 2021 10:41:35 +0000 https://web-tweets.com/?p=405 JavaScriptの非同期処理で、実行される順番がマクロタスクとマイクロタスクでどっちが早いのか・・・となかなか覚えられないので記事に残します。昨今はページ表示速度の兼ね合いで非同期処理は理解が必須。 マクロタスクとマ...

The post JavaScriptの非同期で理解が必要なマクロタスクとマイクロタスクの実行順番と関数 first appeared on 知ってほしいWebのあれこれ.]]>
JavaScriptの非同期処理で、実行される順番がマクロタスクとマイクロタスクでどっちが早いのか・・・となかなか覚えられないので記事に残します。昨今はページ表示速度の兼ね合いで非同期処理は理解が必須。

マクロタスクとマイクロタスクの実行される順番と違い

タスクがどちらにもある場合はマイクロタスクが先に実行

どちらも基本的には非同期処理になるが、マクロタスクとマイクロタスクのそれぞれにタスクが存在する場合、マイクロタスクの方が優先的に実行される。

マイクロタスクは順番がきたら全て実行される

マクロタスクとマイクロタスクのそれぞれにタスクがある場合、マイクロタスクに順番が回ってくる。順番が回ってきたマイクロタスクは全てのジョブを実行する。マイクロタスクのジョブが全て完了したら、マクロタスクに実行フェーズが移る。

マクロタスクは順番が回ってきたら1つずつタスクを実行する

マクロタスクは順番が回ってきたら1つずつタスクを実行する。マクロタスクにマイクロタスクがある場合は、マイクロタスクを全て実行する。マクロタスクが終了したら、その次のマクロタスクを実行する。

マクロタスク(macro tasks)に分類される代表的な関数

  • setTimeout
  • setInterval
  • requestAnimationFrame

マイクロタスク(micro tasks)に分類される代表的な関数

  • Promises
  • queueMicrotask
  • MutationObserver

 

The post JavaScriptの非同期で理解が必要なマクロタスクとマイクロタスクの実行順番と関数 first appeared on 知ってほしいWebのあれこれ.]]>