Article information
RevisionsShow article in Markdown
Report article
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

This post is Private. Only a writer or those who know its URL can access this post.

posted at

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

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);

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Article information
RevisionsShow article in Markdown
Report article
Help us understand the problem. What are the problem?