this・プロトタイプ・class - JavaScript

最終更新日: 2026-07-24

TOP(About this memo)) > 一覧(JavaScript) > this・プロトタイプ・class

this

var sayHelloShared = function() {
    console.log("Hello, I'm " + this.name);
};

var alice = {
    sayHello: sayHelloShared,
    name: 'Alice'
};

var bob = {
    sayHello: sayHelloShared,
    name: 'Bob',
    child: alice
};

alice.sayHello();  // Hello, I'm Alice
bob.sayHello();  // Hello, I'm Bob

// 以下の方法も可能
sayHelloShared.call(alice);  // Hello, I'm Alice
sayHelloShared.call(bob);  // Hello, I'm Bob

new

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

var alice = new Person('Alice', 7);

console.log(alice.name);  // Alice
console.log(alice.age);  // 7
var Person = function(name, age) {
    // var this = {}; を実行
    this.name = name;
    this.age = age;
    // return this; を実行
};
var Person = function(name, age) {
    this.name = name;
    this.age = age;
    return name;
};
console.log(alice.constructor == Person);  // true
var alice = {
    constructor: Person,
    name: 'Alice',
    age: 7
}

プロトタイプ

function Animal() {
    this.cry = "meow";
}
Animal.prototype.shout = function() { // 関数が持つプロトタイプは直接参照できる
    alert(this.cry);
}
var animal = new Animal();
animal.shout();  // -> "meow"

function Bird() {
    this.cry = "sing";
}
Bird.prototype = Object.create(Animal.prototype); // 継承

var niwatori = new Bird();
niwatori.shout();  // -> "sing"
var niwatori = {
    [[Prototype]]: {
        constructor: Birdへの参照
    },
    cry: "sing"
}
function Animal() {
    this.cry = "meow";
    this.shout = function() {
        alert(this.cry);
    };
}
var animal = new Animal();
animal.shout();  // -> "meow"
* この書き方の場合、暗黙的に以下の実行になっているため、`new`のたびに新しい関数オブジェクトが生成されてしまう(プロトタイプの場合は実行時にプロトタイプチェーンで探しにいくため再利用できる)。
function Animal() {
    // var this = {}; を実行
    this.cry = "meow";
    this.shout = function() {
        alert(this.cry);
    };
    // return this; を実行
}

ミックスイン

class

prototypeベースの構文

var Person = function(name) {
    this.name = name;
};
Person.prototype.sayHello = function() {
    console.log("Hello, I'm " + this.getName());
};
Person.prototype.getName = function() {
    return this.name;
};

Class構文

class Person {
    constructor(name) {
        this.name = name;
    }
    sayHello() {
        console.log("Hello, I'm " + this.getName());
    }
    getName() {
        return this.name;
    }
}

継承

class Person {
    constructor(name) {
        this.name = name;
    }
    sayHello() {
        console.log("Hello, I'm " + this.getName());
    }
    getName() {
        return this.name;
    }
}

// Personを継承
// 同じメソッド名であるsayHelloは継承先のもので上書きされる
class Teacher extends Person {
    sayHello() {
        console.log("Hi, I'm " + this.getName());
    }
}

const teacher = new Teacher('soarflat');
teacher.sayHello(); // => Hi, I'm soarflat

コールバック関数内でthisを参照するとき

data() {
    return {
        token: ""
    }
},
mounted() {
    let re_this = this
    grecaptcha.ready(function() {
        grecaptcha.execute('サイトキー').then(function(token) {
            re_this.token = token
        });
    });
}

jQueryの$

// bullyを作ってしまえば、全てのjQueryオブジェクトについてbullyが使える
$.fn.bully = function() { /* ... */ }
$('#' + id).bully();