# JavaScript ãã¿ãŒã³
# ã¯ããã«
# JavaScript ã¯ãªããžã§ã¯ãæå
ãªããžã§ã¯ãïŒ key-value ãã¢ã®ãªã¹ã
ãªããžã§ã¯ãã®çš®é¡
- ãã€ãã£ããªããžã§ã¯ã
- çµã¿èŸŒã¿ãªããžã§ã¯ãïŒArray,Date ãªã©ïŒ
- ãŠãŒã¶å®çŸ©ãªããžã§ã¯ãïŒconst a = {};ïŒ
- ãã¹ããªããžã§ã¯ãïŒwindow ãªã©ïŒ
# ãªãã©ã«ãšã³ã³ã¹ãã©ã¯ã¿
# ãªããžã§ã¯ããªãã©ã«èšæ³
a = {};
ã¡ãªã¿ã«ãããããã空ã®ãªããžã§ã¯ãããšã¯ãObject.prototype ããç¶æ¿ããããããã£ã»ã¡ãœããã®ã¿ãæã€ãªããžã§ã¯ãã®ããš
# çµã¿èŸŒã¿ã³ã³ã¹ãã©ã¯ã¿é¢æ°
䜿ããªããªãã©ã«èšæ³ã䜿ãã
const obj = new Object();
# ã«ã¹ã¿ã ã³ã³ã¹ãã©ã¯ã¿é¢æ°
new
ã䜿ããšå®éã«ã¯äžèšã®ãããªåŠçãè¡ããã
const Person = function(name) {
// æé»çã«ãªããžã§ã¯ããäœæ
// const this = Object.create(Person.prototype);
this.name = name;
// æé»çã«ãªããžã§ã¯ããè¿ã
// return this;
};
Person.prototype.say = function() {
return 'I am ' + this.name;
};
ã³ã³ã¹ãã©ã¯ã¿å
ã§æ瀺çã«ãªããžã§ã¯ããreturn
ããäºãã§ããã
ãã®å Žåããããã¿ã€ãã®ç¶æ¿ã¯æåã§è¡ãå¿
èŠãããã
ãŸããæé»çã«äœæããããªããžã§ã¯ãã¯ç Žæ£ãããã
const Person = function(name) {
return { name: 'john' }; // Person.prototypeã¯ç¶æ¿ãããªã
};
# new ãå¿ãããšãã®ããã®ä¿éº
const Person = function() {
if (!(this instanceof Person)) {
return new Person();
}
// ...something...
};
# é åãªãã©ã«
é åãªãã©ã«ã䜿ããé åã³ã³ã¹ãã©ã¯ã¿ã¯äœ¿ããªã
# æ£èŠè¡šçŸãªãã©ã«
æ£èŠè¡šçŸãªãã©ã«ã䜿ããæ£èŠè¡šçŸã³ã³ã¹ãã©ã¯ã¿ã¯äœ¿ããªã
# ããªããã£ãã®ã©ãããŒ
string, number, boolean ã«ã¯ã©ãããååšãããããªããã£ãã¯ãå³æã«ãªããžã§ã¯ããšãšããŠæ¯ãèããã®ã§ãæ瀺çã«ã©ããã䜿ãæ©äŒã¯ãã»ãŒãªãã
'abc'.toUpperCase();
(22 / 7).toPrecision(3);
䜿ãå¿ èŠãããã®ã¯ãåŒæ°ãããªããã£ãã«å€æããããšãã ãã
const num = Number('3');
const str = String(3);
# ãšã©ãŒãªããžã§ã¯ã
Error()
,SyntaxError()
,TypeError()
ãªã©ãçµã¿èŸŒã¿ã®ãšã©ãŒã³ã³ã¹ãã©ã¯ã¿ãååšããã- ãããã¯ã
throw
ãšãšãã«äœ¿çšãããã - äœæããããªããžã§ã¯ãã¯å¿
ã
name
ãšmessage
ã®ããããã£ãæã£ãŠããããã以å€ã®ããããã£ã¯ãã©ãŠã¶æ¡åŒµã®ãããååšã¯ä¿¡é Œã§ããªãã throw
ã«ã¯ç¬èªã®ãªããžã§ã¯ããšãæå®ããããšãã§ããã
throw new Error('some message'); // newã¯ãªããŠãåäœã¯åã
throw {
name: 'MyError',
message: 'something bad happend',
};
# é¢æ°
- é¢æ°ãšã¯ãå®è¡å¯èœãªãªããžã§ã¯ãã®ããšã§ããã
- é¢æ°ã¯ããŒã«ã«ã¹ã³ãŒããæäŸãã
# Expression ãš Statement
é¢æ°ã®äœæã«ã¯ 3 çš®é¡ã®æ¹æ³ãããã
// named function expression(ååä»ãé¢æ°åŒ)
const add = function add() {};
// anonymous function expression(ç¡åé¢æ°åŒ)
const add = function() {};
// function statement(é¢æ°å®£èš)
function add() {}
# 䜿ãåã
- Expression
- å€æ°ãããããã£ã«ä»£å ¥ããããšã
- åŒæ°ãšããŠæž¡ããããšã
- Statement
- ãã®ä»ã®å Žå
- Statement ã¯å¿ ãããã°ã©ã ã³ãŒãïŒé¢æ°æ¬äœã®äžãããã¯ã°ããŒãã«ç©ºéïŒã«ã®ã¿ååšãã
# Named Function Expression ãš Function Statement ã®éã
- Expression ã¯å€æ°ã®å®£èšã®ã¿å·»ãäžãããã(undefined)
- Statement ã¯ãã¡ã³ã¯ã·ã§ã³ã®å®çŸ©ãå·»ãäžãããã
a(); // => ok
b(); // => errorã«ãªããb===undefined ã ããã
function a() {}
const b = function() {};
# name ããããã£
function foo(); // foo.name === 'foo'
const baz = function baz2(){}; // baz.name === 'baz2'
const bar = function(){}; // bar.name === 'bar'
named function ã«ããŠãããšããããã°æã«ãã¡ã³ã¯ã·ã§ã³åã衚瀺ãããã®ã§äŸ¿å©ã
# ã³ãŒã«ããã¯ãã¿ãŒã³
äžèšã®ç®çã§äœ¿ã
- é¢æ°ã®æ±çšæ§ãé«ãããã
â ãåŠçæ¹æ³ãå€ããã€ã³ãžã§ã¯ãã§ããããã«ããããšã§ãã³ãŒã«ããã¯ãå·®ãæ¿ããã°ã©ããªåŠçã§ãã§ããããã«ããŠãã - éåæã€ãã³ããæ±ããã
â ãã€ãã³ãçºçæã«è¡ãããåŠçãã³ãŒã«ããã¯ãšããŠãªã¹ãã«æž¡ããŠãã
# èªå·±å®çŸ©é¢æ°
1 åããã®æºåäœæ¥ãè¡ãæã«äœ¿ããèªèº«ãæžãæãããããŸã䜿ãããšããªãããã
let scareMe = () => {
console.log('Boo!');
scareMe = () => {
console.log('Double Boo!');
};
};
scareMe(); // => Boo
scareMe(); // => Double Boo
# å³æé¢æ°ïŒIIFEïŒ
ã°ããŒãã«ç°å¢ãæ±ããã«åæã»ããã¢ãããããéã«ãã䜿ãããã
(function(global) {
const a = 1; // ããŒã«ã«å€æ°
// ããã§globalãæäœ
})(this);
IIFE ã¯å€ãè¿ãã®ã§ãã¯ããŒãžã£ãäœãæã«äŸ¿å©
const a = (function() {
return 1;
})();
# å³æãªããžã§ã¯ãåæå
immediate object initialization ãšããã IIFE ãšåãããã°ããŒãã«ãæ±ããã«åæèšå®ãè¡ãããã«äœ¿ãã
({
a: 1,
b: 2,
init: () => {
/* äœããã®åæåäœæ¥ */
},
}.init());
# é¢æ°ããããã£ã«ããã¡ã¢åããŒã¿ã³
ãã¡ã³ã¯ã·ã§ã³ã®å®è¡çµæãããã¡ã³ã¯ã·ã§ã³ã®ããããã£(äŸãã°cache
ãªã©)ã«ä¿æããŠãããã¿ãŒã³ã
éããåŠçãç¡é§ã«è¡ããªãããã«äœ¿ãã
const myFunc = param => {
// ãã£ãã·ã¥ãããã°ãããè¿ã
if (myFunc.cache[param]) return myFunc.cache[param];
/* éããåŠçãªã© */
const result = 'some result';
myFunc.cache[param] = result;
return result;
};
// ãã£ãã·ã¥ã®åæå
myFunc.cache = {};
# èšå®ãªããžã§ã¯ã
é¢æ°ã®åŒæ°ããªããžã§ã¯ãã§æž¡ãæ¹æ³ã
- ã¡ãªãã
- é åºãæ°ã«ããªããŠãã
- ãªãã·ã§ã³ã®ãã©ã¡ãŒã¿ãå®å šã«çç¥ã§ãã
- èªã¿ãããä¿å®ã楜
- ãã©ã¡ãŒã¿ã®è¿œå ã»åé€ã容æ
- ãã¡ãªãã
- ãã©ã¡ãŒã¿ã®ååãèŠããå¿ èŠããã
- ããããã£åããããã¡ã€ãããªã
# ã«ãªãŒå
# é¢æ°ã®é©çš
- é¢æ°ããã°ã©ãã³ã°ã«ãããŠãé¢æ°ã¯åŒã³åºãïŒInvokeïŒããããã®ã§ã¯ãªããé©çšïŒApplyïŒããããã®ã§ããã
- é¢æ°ã®é©çšã«ã¯
Function.prototype.apply()
ã䜿ãã- 第äžåŒæ°ã«ã¯ã
this
ã«ããããªããžã§ã¯ããæå®ãã - 第äºåŒæ°ã«ã¯ãæž¡ãããåŒæ°ã Array ã§æå®ãã
- 第äžåŒæ°ã«ã¯ã
const alien = {
name: 'UFO',
sayHi: function(message) {
console.log(this.name, message);
},
};
alien.sayHi.apply(null, ['hello']); // undefined hello
alien.sayHi.apply(alien, ['hello']); // UFO hello
alien.sayHi('hello'); // UFO hello <= ããã¯ãåè¡ã®ã·ã³ã¿ãã¯ã¹ã·ã¥ã¬ãŒãšèšãã
- äžèšã®äŸããããé¢æ°ã®åŒã³åºããã¯ããé¢æ°ã®é©çšãã®ã·ã³ã¿ãã¯ã¹ã·ã¥ã¬ãŒã«éããªããšèšããã
call()
ããapply()
ã®ã·ã³ã¿ãã¯ã¹ã·ã¥ã¬ãŒã§ããã
# éšåé©çšãšã«ãªãŒå
TODOïŒããããããªãããã£ããäžèšã®æãïŒ
ã«ãªãŒå : f(a, b, c) â g(a)(b)(c)
éšåé©çš : f(å€ 1, å€ 2, c) â g(c)
# ãªããžã§ã¯ãäœæã®ãã¿ãŒã³
# åå空éãã¿ãŒã³
奜ããªååãã€ããã°ããŒãã«ãªããžã§ã¯ããäžã€ã ãçšæããå€æ°ãé¢æ°ãããã®ãªããžã§ã¯ãã«ä»äžããŠãããã¿ãŒã³
# äŸåé¢ä¿ã®å®£èš
äŸåããã¢ãžã¥ãŒã«ããé¢æ°ãã¢ãžã¥ãŒã«ã®åé ã§å®£èšããŠãããšè¯ããåã蟌ãå¿ èŠã®ããã¢ãžã¥ãŒã«ãã²ãšç®ã§åããããã
ãã®éãããŒã«ã«å€æ°ãäœæããŠã¢ãžã¥ãŒã«ãæããŠããããšã§ãèªã¿èŸŒã¿ãé«éã«ãªãããŸããããã¡ã€ãããããªãã
const myFunc = () => {
const event = Yahoo.util.Event;
};
# ãã©ã€ããŒãã¡ã³ã
ååãšããŠãJavascript ãªããžã§ã¯ãã®ã¡ã³ãã¯å šãŠãããªãã¯ã§ããã 以äžã§ã¯ãããããã£ãã¡ãœãããæ¬äŒŒçã«ãã©ã€ããŒãã«ããæ¹æ³ã説æããã
# ã³ã³ã¹ãã©ã¯ã¿ã§ãã©ã€ããŒãã¡ã³ããäœã
ãã©ã€ããŒãã¡ã³ããäœæããããšãã¯ãã³ã³ã¹ãã©ã¯ã¿ã§ã¯ããŒãžã£ã䜿ãã
function Car() {
// private
const name = 'bmw';
// public
this.getName = function() {
console.log(name);
};
}
ã¯ã©ã¹ã ãšãããªããã
class Car {
constructor() {
// private
const name = 'bmw';
// public
this.getName = function() {
console.log(name);
};
}
}
- ããã§ã®
getName()
ã®ããã«ããã©ã€ããŒããªå€æ°ãžã®ã¢ã¯ã»ã¹æš©ãæã€ã¡ãœããããç¹æš©ã¡ãœãããšåŒã¶ããšãããã - ãªããžã§ã¯ããé åã Return ããŠããŸããšåç §ãè¿ãã®ã§ããã©ã€ããŒãå€æ°ãå€æŽããããšãå¯èœã«ãªã£ãŠããŸãããããåé¿ããã«ã¯ããªããžã§ã¯ããã¯ããŒã³ããŠè¿ãããå¿ èŠæäœéã®ããªããã£ãã®ã¿ãè¿ãããã«ããããšãïŒPOLA ã®æ³åïŒPrinciple Of Leart Authority - æå°æš©éã®æ³åïŒ
# ãªããžã§ã¯ããªãã©ã«ã§ãã©ã€ããŒãã¡ã³ããäœã
ãªããžã§ã¯ããªãã©ã«ã§ãã©ã€ããŒããªã¡ã³ããäœãã«ã¯ãç¡åå³æé¢æ°ãäœæããã¯ããŒãžã£ãå©çšãããïŒïŒã¢ãžã¥ãŒã«ãã¿ãŒã³ãšããïŒ
let obj;
(function() {
const name = 'john';
obj = {
getName: function() {
return name;
},
};
})();
console.log(obj.getName());
ãŸãã¯
const obj = (function() {
const name = 'john';
return {
getName: function() {
return name;
},
};
})();
console.log(obj.getName());
# ãããã¿ã€ãã䜿ã£ãŠãã©ã€ããŒãã¡ã³ããäœã
ã³ã³ã¹ãã©ã¯ã¿ã§ãã©ã€ããŒãã¡ã³ããäœããšããã¹ãŠã®ã€ã³ã¹ã¿ã³ã¹ã«éè€ããŠã¡ã³ããäœæãããŠããŸããå¹çãæªãã ãããé¿ããã«ã¯ããããã¿ã€ãã䜿ã£ãŠãã©ã€ããŒãã¡ã³ããäœæããã
function Car() {}
Car.prototype = (function() {
const secret = 'my-secret';
return {
getSecret: function() {
return secret;
},
};
})();
const car = new Car();
console.log(car.getSecret()); // => 'my-secret'
# ãã©ã€ããŒãé¢æ°ãé瀺ãã
ãã©ã€ããŒãã¡ãœããããããªãã¯ã¡ãœãããšããŠé瀺ããæ¹æ³ïŒ=é瀺ãã¿ãŒã³ïŒã ä»®ã«ãããªãã¯ãªã¡ãœãããæžãæããããŠãããã©ã€ããŒããªã¡ãœããã¯ç¡äºã§ãããïŒã€ãã€ã䜿ãã¿ã¡ãããããªãïŒ
let myUtil;
(function() {
function a() {}
function b() {}
function c() {}
myutil = { a, b, c };
});
# ã¢ãžã¥ãŒã«ãã¿ãŒã³
æ©èœããšã«ããããå®çµããããã€ãã®ã³ãŒã矀ã«åããæ¹æ³ã以äžã®æ©èœãçµã¿åãããŠå®çŸããã
- åå空é
- å³æé¢æ°
- ãã©ã€ããŒãã¡ã³ããšç¹æš©ã¡ãœãã
- äŸåé¢ä¿ã®å®£èš
# ãªããžã§ã¯ããäœæããã¢ãžã¥ãŒã«
// åå空é
MYAPP = { utilities: {} };
// å³æé¢æ°ã§ã¯ããŒãžã£ãäœæ
MYAPP.utilities.array = (function(global) {
// äŸåé¢ä¿ã®å®£èš
const utilObj = MYAPP.utilities.object;
const utilLang = MYAPP.utilities.lang;
// ãã©ã€ããŒãã¡ã³ã
const privateVar = 1;
const privateMethod = function() {};
// äžåºŠéãã®åæååŠç
doSomething();
// ãããªãã¯API
return {
// ããã§ãã©ã€ããŒãã¡ã³ãã䜿ã£ãŠãªã«ããè¡ã
somePublicMethod: function() {},
somePublicMethod2: function() {},
};
})(global); // globalãæž¡ããŠã¯ããŒãžã£å
éšã§äœ¿ãããšãå€ã
# ã³ã³ã¹ãã©ã¯ã¿ãäœæããã¢ãžã¥ãŒã«
MYAPP = { utilities: {} };
MYAPP.utilities.Array = (function() {
/* ãã®éšåã¯å
äŸãšåã */
// ãããªãã¯APIãã³ã³ã¹ãã©ã¯ã¿
const Constructor = function() {
// ããã§ãã©ã€ããŒãã¡ã³ãã䜿ã£ãŠãªã«ããè¡ã
};
// ãããªãã¯APIããããã¿ã€ã
Constructor.prototype = {
// ããã§ãã©ã€ããŒãã¡ã³ãã䜿ã£ãŠãªã«ããè¡ã
};
return Constructor;
})(global);
# ãµã³ãããã¯ã¹ãã¿ãŒã³
ã¢ãžã¥ãŒã«ãã¿ãŒã³ãæã€ä»¥äžã®æ¬ ç¹ãå æããããã«äœ¿ãã
- åãåå空éåãåæã«äœ¿ããªãïŒããŒãžã§ã³éããªã©ã®ãã¹ããããã«ããïŒ
- ååãé·ããããïŒMYAPP.utilities.array ãªã©ïŒ
ãµã³ãããã¯ã¹ç°å¢ã«ã¢ãžã¥ãŒã«ïŒæ©èœïŒãã€ã³ãžã§ã¯ããããµã³ãããã¯ã¹å ã§åŠçãå®çµããã AngularJS ãšåããã¿ãŒã³ããã
- ã€ã³ã¹ã¿ã³ã¹ãäœæãã
- ã€ã³ã¹ã¿ã³ã¹ã«æ©èœãã€ã³ãžã§ã¯ããã
- ã³ãŒã«ããã¯é¢æ°å
ã§ã€ã³ã¹ã¿ã³ã¹ãæäœããŠå¿
èŠãªåŠçãè¡ãïŒäžèšã§èšã
box
ïŒ
詳现ãªã³ãŒãã¯ãæ¬æžã® 5.5.2, 5.5.3 ãåç §ããããšã
new Sandbox(['module1', 'module2'], function(box) {
// `box`ã«ã¯ããã§ã«module1/module2ã®æ©èœãã€ã³ãžã§ã¯ããããŠããã
// ããã§ããããã®æ©èœã掻çšããŠå¿
èŠãªåŠçãè¡ã
});
function Sandbox(modules, callback) {
/* thisã«å¯ŸããŠSandbox.modules.module1ãªã©ãã¢ã¿ããããåŠçãè¡ã£ãã®ã¡ãcallbackãã */
callback(this);
}
// ã¢ãžã¥ãŒã«ã¯ã³ã³ã¹ãã©ã¯ã¿é¢æ°ã«ã¢ã¿ããããŠãã
Sandbox.modules.module1 = function something();
Sandbox.modules.module2 = function something2();
# ã¹ã¿ãã£ãã¯ã¡ã³ã
ã¹ã¿ãã£ãã¯ïŒã¯ã©ã¹å šäœã§å ±æãããã®
# ãããªãã¯ãªã¹ã¿ãã£ãã¯ã¡ã³ã
const Car = function() {};
// ã¹ã¿ãã£ãã¯ã¡ãœãã
Car.ride = function() {};
// ã€ã³ã¹ã¿ã³ã¹ã¡ãœãã
Car.prototype.sayName = function() {};
const bmw = new Car();
Car.ride(); // ok
Car.sayName(); // bad
bmw.ride(); // bad
bmw.sayName(); // ok
ã€ã³ã¹ã¿ã³ã¹ããã¹ã¿ãã£ãã¯ã¡ãœããã«ã¢ã¯ã»ã¹ãããæã¯ããããã¿ã€ãã«ã¹ã¿ãã£ãã¯ã¡ãœãããè¿œå ããŠããã ãã®å Žåãã¹ã¿ãã£ãã¯ã¡ãœããå ã§ãã¹ã¿ãã£ãã¯ã«åŒã°ããã®ãã€ã³ã¹ã¿ã³ã¹ããåŒã°ããã®ããå€å®ããé©åãªåå²åŠçãè¡ãå¿ èŠãããã
Car.ride = function() {
if (this instanceof Car) {
/* ã€ã³ã¹ã¿ã³ã¹ã¡ãœãããšããŠåŒã³åºãããå Žåã®åŠç */
}
};
Car.prototype.sayName = Car.ride;
# ãã©ã€ããŒããªã¹ã¿ãã£ãã¯ã¡ã³ã
const Car = (function() {
let count = 0; // ãã©ã€ããŒããªã¹ã¿ãã£ãã¯ã¡ã³ã
// returnããã³ã³ã¹ãã©ã¯ã¿
const NewCar = function() {
couner += 1; // newããããã³ã«ã¹ã¿ãã£ãã¯ã¡ã³ããå ç®ãã
};
// ã¹ã¿ãã£ãã¯ã¡ã³ããååŸããç¹æš©ã¡ãœãã
NewCar.prototype.getLastId = function() {
return counter;
};
return NewCar;
})();
const car1 = new Car();
const car2 = new Car();
car1.getLastId(); // 1
car2.getLastId(); // 2
# ãªããžã§ã¯ãå®æ°
Javascript ã«å®æ°ã¯ãªããããããå®æ°ãéçããããã£ãšããŠã³ã³ã¹ãã©ã¯ã¿é¢æ°ã«è¿œå ããããšã¯ããè¡ãããã
Car.MAX_HEIGHT = 1500;
Car.MAX_WIDTH = 1780;
# é£éïŒChainïŒãã¿ãŒã³
åºæ¬çã«ããªããžã§ã¯ãã®ã¡ãœããã¯this
ãè¿ãããã«ããŠãããšããïŒæå³ã®ããå€ãè¿ãå¿
èŠãããå Žåãé€ãïŒã
ããã«ãããã¡ãœããã®ãã§ãŒã³ãå¯èœã«ãªãã
- ã¡ãªãã
- ã¿ã€ãã³ã°éã®æžå°
- ç°¡æœã§æã®ããã«èªãã
- åŠçãåå²ã§ãä¿å®æ§ãé«ã
- ãã¡ãªãã
- ãããã°ãé£ãããªã
const obj = {
value: 1,
increment: function() {
this.value++;
return this;
},
decrement: function() {
this.value--;
return this;
},
};
console.log(
obj
.increment()
.increment()
.decrement().value, // 2
);
# ã³ãŒãåå©çšãã¿ãŒã³(ã¯ã©ã·ã«ã«)
- prototype ããããã£ã¯é¢æ°ã§ã¯ãªããªããžã§ã¯ãã§ããå¿ èŠããããããã¯éèŠãªããšãªã®ã§ããèŠããããšã
# äž¡æ¹ãç¶æ¿
Child.prototype = new Parent();
- ããããã£ã»ãããã¿ã€ãã®äž¡æ¹ãç¶æ¿ãããã¿ãŒã³
- ãã®ãã¿ãŒã³ã§ã¯ã芪ã®ã€ã³ã¹ã¿ã³ã¹ã¡ã³ãïŒäŸïŒ
name
ïŒãšãããã¿ã€ãã¡ã³ãïŒäŸïŒsay
ïŒããå šãŠåã«ç¶æ¿ãããããã ãããã¯ãããã¿ã€ãé£éã«ãããã®ã§ãã³ããŒãããŠããããã§ã¯ãªãã - æ¬ ç¹
- 芪ã®ã€ã³ã¹ã¿ã³ã¹ã¡ã³ããç¶æ¿ãããããããã¯äžèŠãªããšãå€ã
- åãã芪ã«ãã©ã¡ãŒã¿ãæž¡ããªã
// 芪
function Parent() {
this.name = 'Adam';
}
Parent.prototype.say = function() {
return this.name;
};
// å
function Child() {}
// ç¶æ¿
Child.prototype = new Parent();
const child = new Child();
child.say(); // Adam
äžèšã®äŸã§ã¯ãããããã£ãã¡ãœããã¯äžèšã®é ã«é¡ã£ãŠæ€çŽ¢ãããã
- Child ã€ã³ã¹ã¿ã³ã¹
- Child ã³ã³ã¹ãã©ã¯ã¿ã®ãããã¿ã€ãïŒParent ã€ã³ã¹ã¿ã³ã¹ïŒ
- Parent ã³ã³ã¹ãã©ã¯ã¿ã®ãããã¿ã€ã
# äž¡æ¹ãç¶æ¿ïŒã³ã³ã¹ãã©ã¯ã¿æåïŒ
function Child(...args) {
Parent.apply(this, args);
}
Child.prototype = new Parent();
- ããããã£ã»ãããã¿ã€ãã®äž¡æ¹ãç¶æ¿ãããã¿ãŒã³
- åé ãšç°ãªããã³ã³ã¹ãã©ã¯ã¿ãæåããããšã§ã芪ã®ã€ã³ã¹ã¿ã³ã¹ã¡ã³ãã¯åã«ã³ããŒããã
- 芪ã®ãããã¿ã€ãããé£éã«ããåã«ç¶æ¿ããã
// 芪
function Parent(name) {
this.name = name || 'Adam';
}
Parent.prototype.say = function() {
return this.name;
};
// å
function Child(...args) {
Parent.apply(this, args);
}
Child.prototype = new Parent();
const child = new Child('Patrick');
console.log(child.name); // Patrick
console.log(child.say()); // Patrick
# ããããã£ã®ã¿ç¶æ¿
function Child(...args) {
Parent.apply(this, args);
}
ã³ã³ã¹ãã©ã¯ã¿ãæåããŠãããããã£ã®ã¿ç¶æ¿ïŒã³ããŒïŒããã
- 芪ã®ã€ã³ã¹ã¿ã³ã¹ã¡ã³ãïŒäŸïŒ
name
ïŒã®ã¿ç¶æ¿ãããã芪ã®ãããã¿ã€ãã¡ã³ãã¯ç¶æ¿ãããªãã - å©ç¹
- 芪ã®ã€ã³ã¹ã¿ã³ã¹ã¡ã³ãã®ã³ããŒãåŸããã
- æ¬ ç¹
- 芪ã®ãããã¿ã€ããããªã«ãç¶æ¿ãããªãã®ã§ãé£éã¯å£ãã
// 芪
function Parent() {
this.name = 'Adam';
}
// å
function Child(...args) {
Parent.apply(this, args);
}
const child = new Child();
console.log(child.name); // Adam
console.log(child.hasOwnProperty('name')); // true
# ãããã¿ã€ãã®ã¿ç¶æ¿ïŒå ±æïŒ
Child.prototype = Parent.prototype;
- å©ç¹
- éããã·ã³ãã«
- æ¬ ç¹
- äžç®æã®ãããã¿ã€ãã®å€æŽãããã¹ãŠã®åã»èŠªã«åœ±é¿ãäžããŠããŸãã
# ãããã¿ã€ãã®ã¿ç¶æ¿ïŒãããã·ïŒ
ãã¿ãŒã³ 4 ãæ¹åãããã®ã äžæçã³ã³ã¹ãã©ã¯ã¿ã«ãããããã·ãããŸããããšã§ãå ç¥ã®ãããã¿ã€ããå€æŽãããŠããŸãããšãé²ãã§ããã ããããã»ãŒãã¹ããšèšããã
const Proxy = function() {};
Proxy.prototype = Parent.prototype;
Child.prototype = new Proxy();
Child.prototype.constructor = Child; // ParentãããªãããŒãšæ瀺
# ã³ãŒãåå©çšãã¿ãŒã³(ã¢ãã³)
# ãããã¿ã€ãã®ã¿ç¶æ¿
Object.create()
(opens new window)ã䜿ãã
第äžåŒæ°ã«ãããã¿ã€ããšãªãã¹ããªããžã§ã¯ããã第äºåŒæ°ã«Properties Object (opens new window)ãèšèŒãã
// 芪
function Parent(name) {
this.name = name || 'Adam';
}
Parent.prototype.say = function() {
return this.name;
};
const parent = new Parent();
const child = Object.create(parent, { age: { value: 17 } });
console.log(child.hasOwnProperty('name')); // false => prototype
console.log(child.hasOwnProperty('say')); // false => prototype
console.log(child.hasOwnProperty('age')); // true
console.log(child.say()); // Adam
äžèšã®äŸã¯ãã¯ã©ã·ã«ã«ãã¿ãŒã³ïŒãšåãåäœã«ãªãã
const child = Object.create(Parent.prototype, { age: { value: 17 } });
äžèšã®äŸã¯ãã¯ã©ã·ã«ã«ãã¿ãŒã³ 4 ãšåãåäœã«ãªãã
# ããããã£ã®ã¿ç¶æ¿
åçŽã«èŠªã®ããããã£ãåã«ã³ããŒãããäžèšã®äºçš®é¡ãããã
- Shallow CopyïŒ
Object.assign()
ãªã©ïŒ - Deep Copy (lodash ã® deep clone ãªã©)
# ããã¯ã¹ã€ã³
è€æ°ã®ãªããžã§ã¯ããæ··ãåãããŠæ°ãããªããžã§ã¯ããäœããå®è£ ã¯ç°¡åã§ãåã«ããããã£ãã³ããŒããŠããã ãã§ããã
const cake = mix(
{ egg: 2, large: true },
{ butter: 1, salted: true },
{ sugar: 'sure!' },
);
# ã¡ãœããã®æå
ãããªããžã§ã¯ãã®ã¡ããœãã䜿ããããããã芪åé¢ä¿ã¯äœããããªãããããããšãã¯ã¡ãœããã®æåã䜿ããšäŸ¿å©ã
å
è¿°ã®éããapply
ãcall
ã䜿ãã
function slice(...args) {
return Array.prototype.slice.apply(args, [1, 3]);
}
console.log(slice(1, 2, 3, 4, 5, 6)); // 2, 3
# æçž
ãããªããžã§ã¯ãã®ãã¡ã³ã¯ã·ã§ã³ã« this ããã€ã³ãããããšãã¯ãäžèšã®ããã«ããïŒES5 以åïŒ
function bind(obj, func) {
return function(...args) {
return func.apply(obj, args);
};
}
newFunc = bind(object, someObject.oldFunc);
ES5 移è¡ã§ã¯ãFunction.prototype.bind()
ã䜿ãããšã§æçžã§ããã
第äžåŒæ°ã« this ãšãªãã¹ããªããžã§ã¯ããæå®ããã
第äºåŒæ°ä»¥éãæå®ããããšã§éšåé©çšãè¡ãããšãã§ããã
someObject.oldFunc.bind(object, arg1, arg2);
# ãã¶ã€ã³ãã¿ãŒã³
# ã·ã³ã°ã«ãã³
JavaScript ã«ã¯ã¯ã©ã¹ããªãããªããžã§ã¯ãèªäœããã§ã«ã·ã³ã°ã«ãã³ã§ããã
obj1 = {};
obj2 = {};
obj1 === obj2; // false
以äžãå®éã«ã¯èš³ã«ã¯ç«ããªãããå®éšçã«å®è£ ããŠã¿ãäŸã
# new ã䜿ã£ãŠã·ã³ã°ã«ãã³ãäœãïŒã¹ã¿ãã£ãã¯ããããã£ã«ãã£ãã·ã¥ïŒ
function Car() {
if (typeof Car.cache === 'object') {
return Car.cache;
}
// do something like `this.name = 'bmw'`
Car.cache = this;
}
console.log(new Car() === new Car()); // true
# new ã䜿ã£ãŠã·ã³ã°ã«ãã³ãäœãïŒã¯ããŒãžã£ã«ãã£ãã·ã¥ïŒ
å äŸã§ã¯æžãæããå¯èœãªã®ã§ãæžãæããã§ããªããããã¿ãŒã³ã
const Car = (function() {
// ãã©ã€ããŒãã¡ã³ã
let instance;
// ç¹æš©ã¡ãœããïŒã³ã³ã¹ãã©ã¯ã¿ïŒãè¿ã
return function() {
// ãã£ãã·ã¥ãããã°ãããè¿ããŠçµãã
if (instance) return instance;
// ãã£ãã·ã¥ããŠãã
instance = this;
// this.name = 'something'ãªã©ã®åŠçãããã§ãã
};
})();
console.log(new Car() === new Car()); // true
# ãã¡ã¯ããª
å®è¡æã«æååãšããŠæå®ãããåã§ãªããžã§ã¯ããäœæããããã®ã¡ãœããã
- ã¯ã©ã¹ãŸãã¯ã¯ã©ã¹ã®éçã¡ãœããã§å®è£ ãã
- ãã¡ã¯ããªã§äœæãããªããžã§ã¯ãã¯ãå ±éã®èŠªãç¶æ¿ãã
- 以äžã®ã¡ãªããããã
- 䌌ããããªãªããžã§ã¯ãã楜ã«éç£ã§ããïŒ
new
ããªããžã§ã¯ããªãã©ã«ã䜿ããªã㧠OKïŒ - åãç¥ããªããŠããªããžã§ã¯ããäœæã§ããæ段ãæäŸãã
- 䌌ããããªãªããžã§ã¯ãã楜ã«éç£ã§ããïŒ
function Car() {}
Car.prototype.drive = function() {
console.log(`I have ${this.doors} doors.`);
};
Car.Compact = function() {
this.doors = 4;
};
Car.Convertible = function() {
this.doors = 2;
};
Car.Bus = function() {
this.doors = 20;
};
Car.factory = function(type) {
// äžåºŠã ããããã¿ã€ããç¶æ¿ããåŠç
if (typeof Car[type].prototype.drive) {
Car[type].prototype = new Car();
}
// é©åãªã³ã³ã¹ãã©ã¯ã¿ã§ãªããžã§ã¯ããäœæããŠè¿ã
return new Car[type]();
};
compact = Car.factory('Compact');
convertible = Car.factory('Convertible');
bus = Car.factory('Bus');
compact.drive(); // I have 4 doors.
convertible.drive(); // I have 2 doors.
bus.drive(); // I have 20 doors.
# ã€ãã¬ãŒã¿
è€éãªæ§é ã®ããŒã¿ãã«ãŒãã§å·¡ååŠçããããã«ãå¿
èŠãª API ãå
¬éãããã¿ãŒã³ã
API ã«ã¯ãnext()
,hasNext()
,rewind()
,current()
ãªã©ãçšæããã
ããŒã¿ã¯ãã©ã€ããŒãã«ããããšãå€ãã
iterable = (function() {
let index = 0;
const data = [1, 2, 3, 4, 5];
return {
next: function() {
if (!this.hasNext()) return null;
element = data[index];
index += 1;
return element;
},
hasNext: function() {
return index < data.length;
},
rewind: function() {
index = 0;
},
current: function() {
return data[index];
},
};
})();
console.log(iterable.next()); // 1
console.log(iterable.next()); // 2
console.log(iterable.next()); // 3
console.log(iterable.next()); // 4
console.log(iterable.next()); // 5
console.log(iterable.next()); // null
# ãã³ã¬ãŒã¿
å®è¡æã«åçã«æ©èœãè¿œå ããŠãããã¿ãŒã³ãäžèšã®äŸã§ã¯getPrice()
ã«åçã«æ©èœãè¿œå ããŠããã
function Sale(price) {
this.price = price;
this.queue = [];
}
// ãã³ã¬ãŒã¿ã«ã¯ããã³ã¬ãŒããããé¢æ°ãšååã®é¢æ°ãæããã
Sale.decorators = {
tax: {
getPrice: price => price * 1.05,
},
sending: {
getPrice: price => price + 1500,
},
jpy: {
getPrice: price => `JPY:${price}`,
},
};
// å®è¡æã«ãã³ã¬ãŒã¿ãè¿œå ããããããã¥ãŒã«ä¿åããŠãã
Sale.prototype.decorate = function(decorator) {
this.queue.push(decorator);
};
// ãã¥ãŒãããã³ã¬ãŒã¿ãèªã¿åºãé ã«å®è¡ããŠãã
Sale.prototype.getPrice = function() {
let price = this.price;
for (name of this.queue) {
price = Sale.decorators[name].getPrice(price);
}
console.log(price);
};
const sale = new Sale(10000);
// getPrice()ã«å¯Ÿãåçã«æ©èœãè¿œå ããŠãã
sale.decorate('tax');
sale.decorate('sending');
sale.decorate('jpy');
// å®è¡ãããšããJPY:12000ããåºåããã
sale.getPrice();
# ã¹ãã©ããžãŒ
- å®è¡æã«ããã€ãã®ã¢ã«ãŽãªãºã ïŒæŠç¥ã»ã¹ãã©ããžãŒïŒã®äžããæé©ãªãã®ãéžæãããã¿ãŒã³ã
- validation ãªã©ã§ãã䜿ãããã
const validator = {
// ãããã¹ãã©ããžãŒïŒæŠç¥ïŒ
strtegy: {
isNonEmpty: {
validate: _ => _ !== '',
instructions: 'äœãå
¥åããŠãã ãã',
},
isNumber: {
validate: _ => !isNaN(_),
instructions: 'æ°å€ã«ããŠãã ãã',
},
isAlphaNum: {
validate: _ => !/[^a-z0-9]/i.test(_),
instructions: 'è±æ°åã«ããŠãã ãã',
},
},
// ããŒåïŒé
ç®ïŒããšã«ã¹ãã©ããžãŒãå²ãåœãŠãŠãã
config: {
firstName: 'isNonEmpty', // 空ã¯ã ãã
// lastNameã¯ãªãã§ãããã
age: 'isNumber', // æ°åãããªããšã ãã
username: 'isAlphaNum', // è±æ°åãããªããšãã¡ã
},
messages: [],
validate: function(data) {
this.messages = [];
for (key in data) {
// ãããã¿ã€ãã¯ã¹ã«ãŒ
if (!data.hasOwnProperty(key)) continue;
// ããŒã«é©åããã¹ãã©ããžãŒåãšã¹ãã©ããžãŒæ¬äœãæœåº
const strategyName = this.config[key];
const strategy = this.strtegy[strategyName];
// ãã§ãã¯ã®å¿
èŠããªãããŒã¯ã¹ã«ãŒ
if (!strategyName) continue;
const valid = strategy.validate(data[key]);
if (!valid) {
const msg = `Error at ${key}. ${strategy.instructions}`;
this.messages.push(msg);
}
}
},
hasErrors: function() {
return this.messages.length !== 0;
},
};
const data = {
firstName: '',
lastName: 'Doe',
age: 'unknown',
username: 'o_O',
};
validator.validate(data);
if (validator.hasErrors()) console.log(validator.messages);
/*
[ 'Error at firstName. äœãå
¥åããŠãã ãã',
'Error at age. æ°å€ã«ããŠãã ãã',
'Error at username. è±æ°åã«ããŠãã ãã' ]
*/
# ãã¡ãµãŒã
ãã¡ãµãŒãïŒå»ºç©ã®æ£é¢éšåã®ããšã
çµã¿åãããŠäœ¿ãããã¡ãœãã矀ïŒãããã¯èšèšããŸããã¡ãœãã矀ïŒãæ°ããã¡ãœããã§å ãã§ããã䟿å©ãª API ãæäŸããæ¹æ³ã
// äžç·ã«åŒã¶
myobj = {
superMethod: (arg) => {
method1(arg);
method2(arg);
}
}
// æ¡ä»¶åå²ãã
myobj = {
method: () => {
if() method1();
if() method2();
}
}
// å€ãAPIããŸãšããŠæ°ããAPIãäœã
myobj = {
newAPI: () => {
/* do something new */
// 段éçã«å€ãAPIã¯å»æ¢ãã
oldAPI1();
oldAPI2();
oldAPI3();
},
oldAPI1: () => {},
oldAPI2: () => {},
oldAPI3: () => {},
}
# ãããã·
ããã°ã©ã ãš API ã®éã«ãããã·ãããŸãããã¿ãŒã³ãäžèšã®ãããªç®çã§äœ¿ãã
- é
延åæå
- æ¬åœã«å¿ èŠã«ãªããŸã§ãã³ã¹ãã®æããäœæ¥ããã³ãã£ã³ã°ãã
- ãªã¯ãšã¹ãã®éçŽ
- bounce çã掻çšããŠããªã¯ãšã¹ãããŸãšããŠéä»ããããšã§å¹çãäžãã
- cache
- ãã£ãã·ã¥ãããã°ããã䜿ã
# ã¡ãã£ãšãŒã¿ïŒä»²ä»è ïŒ
ã¢ãžã¥ãŒã«éãççµåã«ä¿ã€ããã®ãã¶ã€ã³ãã¿ãŒã³ã
ã¢ãžã¥ãŒã«éã§çŽæ¥ã®ãããšãã¯ããã«ãå šãŠã¡ãã£ãšãŒã¿ãšåŒã°ãã仲ä»ã¢ãžã¥ãŒã«ãä»ããŠè¡ãæ¹æ³ã
# ãªãã¶ãŒã
PublishïŒçºè¡ïŒ/SubscribeïŒè³ŒèªïŒãã¿ãŒã³ãšãããããªãã¶ãŒãå¯èœãªãªããžã§ã¯ããäœæããããšã§ããªããžã§ã¯ãéãççµåã«ä¿ã€ã
- çºè¡è ã¯ãPublisher(å㯠Subject, node ã§ã¯ EventEmitter ãªã©)ãšåŒã°ãããRx ã® Subject ãšã¯æå³ãç°ãªãã®ã§æ³šæã
- 賌èªè ã¯ãSubscriber(å㯠Observer)ãªã©ãšåŒã°ããã
Publisher ã¯äžèšã®ã¡ã³ããæã€
subscribers
ãè¡åè ãä¿æããé åsubscribe(type, cb)
åã¯on(type, cb)
賌èªè ãè¿œå ããunsubscribe(type, cb)
賌èªè ãåé€ããpublish|emit(type, arg)
ã€ãã³ããçºè¡ããå賌èªè ã«éç¥ãã
# DOM ãšãã©ãŠã¶ã®ãã¿ãŒã³
# é¢å¿ã®åé¢
é¢å¿ãäžèšã®éãã«åãã段éç匷åïŒProgressive EnhansementïŒãè¡ãããšããèãæ¹ã
- ã³ã³ãã³ã㯠HTML
- ãã¬ãŒã³ããŒã·ã§ã³ã¯ CSS
- æ¯ãèã㯠Javascript
# DOM ã¹ã¯ãªããã£ã³ã°
çµè«ïŒDOM èµ°æ»ã¯æå°éã«æžãã
ããŒã¿ã®ååŸ
// ã¢ã³ããã¿ãŒã³
const padding = doument.getElementById('some').style.padding;
const margin = doument.getElementById('some').style.margin;
// ããããæ¹æ³
const style = doument.getElementById('some').style;
const padding = style.padding;
const margin = style.margin;
èŠçŽ ã®è¿œå ïŒFragent ãæ°èŠã«äœããäžããããïŒ
const fragment = document.createDocumentFragment();
fragment.appendChild(someChild);
fragment.appendChild(someChild);
document.body.appendChild(fragment);
èŠçŽ ã®å€æŽïŒã¯ããŒã³ããããšã§ Fragment ãäœããäžããããïŒ
const oldNode = document.getElementById('some');
const cloneNode = oldNode.cloneNode(true);
oldNode.parentNode.replaceChild(cloneNode, oldNode);
# ã€ãã³ãå§è²
<div><a>button1</a> <a>button2</a> <a>button3</a></div>
äžèšã®ãããªèŠçŽ ããã£ããšããA èŠçŽ ã«ã€ãã³ããèšå®ãããDIV èŠçŽ ã«ã€ãã³ããèšå®ããŠäžæ¬ã§åŠçããããšãDIV ã®ã€ãã³ããã³ãã©ã§äžèšã®ããã«åŠçããã
if (e.target.nodeName.toLowerCase() === 'a') doSomething();
# éããåŠç
ãã©ãŠã¶ããã³ã°ããããããªéããåŠçã«ã¯ãããã¯ã°ã©ãŠã³ãã§åäœãã WebWorker ã䜿ããšè¯ãã
worker åŽã§postMessage
ããŠããã©ãŠã¶åŽã®onmessage
ã§åãåãäºãã§ããã
// Browser
const worker = new Worker('myWebWorker.js');
worker.onmessage = e => console.log(e.data); // => 'hello' 'done!'
// Web Worker
postMessage('hello');
setTimeout(() => postMessage('done!'), 1000);
# JS ãã¡ã€ã«ã®èªã¿èŸŒã¿
# script èŠçŽ ãã©ãã«æžãã
script èŠçŽ ã¯ä»ã®ããŠã³ããŒããé»å®³ããã®ã§ãå¯èœãªéã body çµäºã¿ã°ã®è¿ãã«æžããHEAD ã«ãã¹ãŠãŸãšããã®ã¯ææªã®ã¢ã³ããã¿ãŒã³ã
# åçãªã¹ã¯ãªããã®èªã¿èŸŒã¿
const script = document.createElement('script');
script.src = '//www.google.com/some.js';
const firstScriptNode = document.getElementByTagName('script')[0];
firstScriptNode.parentNode.insertBefore(script, firstScriptNode);
SCRIPT èŠçŽ ã®åã«æ¿å ¥ããŠããçç±ã¯ãBODY èŠçŽ ã HEAD èŠçŽ ã¯ååšããªãå¯èœæ§ãããããscript èŠçŽ ã¯çµ¶å¯Ÿã«ååšããèŠçŽ ã ããïŒãªããã°ãã®ã³ãŒãã¯å®è¡ã§ããªãã®ã§ïŒ
# é 延èªã¿èŸŒã¿
JS ããæäœéå¿
èŠãª JS ãšãè£
食ç㪠JS ã«åããŠãåŸè
ã®ã¿ãwindow.load
ã€ãã³ã以éã«åçã«èªã¿èŸŒãæ¹æ³ã
window.onload = () => {
/* åçãªJSã®èªã¿èŸŒã¿ïŒåé
ã®ãšããïŒ */
};
# ãªã³ããã³ãã§èªã¿èŸŒã
å è¿°ã®åçãªã¹ã¯ãªããã®èªã¿èŸŒã¿ãããã°ã©ããã£ãã¯ã«è¡ãã ãããªããèªã¿èŸŒã¿ã®å®äºãè£è¶³ãããå Žåã¯äžèšã®ããã«ããã
script.onload = () => {
/* JSãã¡ã€ã«ã®èªã¿èŸŒã¿å®äºæã«è¡ãããåŠçãããã«ãIEã®å Žåã¯å¥ã®æ¹æ³ãå¿
èŠ */
};