LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

プレゼン 俺とAngularJS 2 の ソースコード部

Posted at

Learn JS in kanazawa - Kakenai.js ver.1.0 において私がプレゼンした 俺とAngularJS 2 で使ってデモ用のソースコードです。

簡単なtypeの便利さを伝える感じです。

Add Demo

数値1と文字列1で結果が変わってしまうことを定時。

var button = document.createElement('button');
button.textContent = "alert add";

function add(a, b) {
    return a + b;
}

var first = 1;
var second = 2;
button.onclick = function () {
    alert( add(first, second) );
}

document.body.appendChild(button);

interface Demo(Fail)

引数で渡ってきたオブジェクトにメソッドがある前提なのに、
無いものも渡せる。

var animal = {
    move: function (meter) {
        alert('move move move ' + meter + 'm');
    }
}

var car = {};

function gogo(mover) {
    mover.move(10);
}


gogo(animal);
gogo(car);


interface Demo

ちゃんとインターフェイスを持ったオブジェクトでは無いとエラーになる。

interface IMove{
    move(m: Number): Number;
};

class Animal implements IMove {
    move(meter: Number): Number {
        alert('move move move ' + meter + 'm');
        return meter;
    }
}

class Car implements IMove {
     move(meter: Number): Number {
        alert('boooom! ' + meter + 'm');
        return meter;
    }   
}

function gogo(mover: IMove) {
    mover.move(10)
}

var animal = new Animal;
var car = new Car;

gogo(animal);
gogo(car);

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