3. •
function hoge () {
}
•
var hoge = function () {
};
4. •
hoge(); // ‘hello’
function hoge () {
return ‘hello’;
}
•
hoge(); // TypeError: undefined is not a function
var hoge = function () {
return ‘hello’;
};
10. var Bird = function (name) {
this.name = name;
};
Bird.prototype = {
nakigoe: ‘piyopiyo’,
say: function () {
return this.name + ‘: ‘ + this.nakigoe;
}
};
11. new
var tori = new Bird(“tori”);
tori.say(); // “tori: piyopiyo”
12. • tori nakigoe name
bird.nakigoe = ‘pugyaaa’;
bird.name = ‘hoge’;
bird.say() // ‘hoge: pugyaaa’;
• new
var tori = Bird('hoge'); // this window
window.name; // “hoge”
13. var bird = function (name) {
var pub = {},
nakigoe = ‘piyopiyo’;
pub.say = function () {
return name + ‘: ‘ + nakigoe;
};
return pub;
};
14. var tori = bird(‘tori’);
tori.say(); // ‘tori: piyopiyo’;
tori.name // undefined
tori.nakigoe // undefined
15. var suzume = function (name) {
var pub = bird(name),
nakigoe = ‘chun chun’;
pub.chun = function () {
return name + ‘: ‘ + nakigoe;
};
return pub;
};
16. var tarou = suzume(‘tarou’);
tarou.say(); // ‘tarou: piyopiyo’;
tarou.chun(); // ‘tarou: chun chun’;