View データ型の変換
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var answer = 42; | |
answer = "いままで魚をありがとう..."; | |
//上記は問題なし |
View 関数のパラメータ(プリミティブな値)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hoge(cnt) { | |
cnt = 3; | |
} | |
var cnt = 1; | |
hoge(cnt); | |
console.log('カウント:' + cnt); //"カウント:1"が表示される |
View 関数のパラメータ(オブジェクト)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myFunc(theObject) { | |
theObject.make = "Toyota"; | |
} | |
var mycar = {make: "Honda", model: "Accord", year: 1998}; | |
var x, y; | |
x = mycar.make; // x は "Honda" という値になる | |
myFunc(mycar); |
View 再帰アルゴリズム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo(i) { | |
if (i < 0) | |
return; | |
console.log('begin:' + i); | |
foo(i - 1); | |
console.log('end:' + i); | |
} | |
foo(3); | |
// 出力内容 |
View 短縮形の関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = [ | |
"Hydrogen", | |
"Helium", | |
"Lithium", | |
"Beryllium" | |
]; | |
var a2 = a.map(function(s){ return s.length }); | |
var a3 = a.map( s => s.length ); |
View オブジェクトのプロパティ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myObj = new Object(), | |
str = "myString", | |
rand = Math.random(), | |
obj = new Object(); | |
myObj.type = "Dot syntax"; | |
myObj["date created"] = "String with space"; | |
myObj[str] = "String value"; | |
myObj[rand] = "Random Number"; | |
myObj[obj] = "Object"; |
View プロパティの巡回
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function showProps(obj, objName) { | |
var result = ""; | |
for (var i in obj) { | |
if (obj.hasOwnProperty(i)) { | |
result += objName + "." + i + " = " + obj[i] + "\n"; | |
} | |
} | |
return result; | |
} |
View 厳密に比較
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//厳密に等しい (===) | |
//厳密等価演算子はオペランド同士が、型を変換することなく (上に示した通り) 厳密に等しいならば真を返します。 | |
3 === 3 // true | |
3 === '3' // false | |
//厳密には等しくない (!==) | |
//厳密不等価演算子は、オペランド同士が等しくない、型が等しくない、あるいはその両方ならば真を返します。 |
View 継承のやり方
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//敵を作る | |
function Teki(){ | |
this.hp = 100; | |
} | |
//ドラゴンを作る | |
function Dragon(){ | |
Teki.apply(this,arguments); | |
} | |
Dragon.prototype = new Teki; |
View 継承のサンプルソース
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//敵を作る | |
function Teki(n){ | |
this.name = n; | |
} | |
//現れる | |
Teki.prototype.appear = function(){ | |
console.log(this.name+"が あらわれた!"); | |
}; | |
//攻撃 | |
Teki.prototype.attack = function(){ |