JavaScriptã¯ããã«ãã¦prototypeãæ¨ã¦ã¯ã©ã¹ãã¼ã¹ç¶æ¿ãå¾ãã®ã
ãã£ããã¯ãprototype 㨠constructor 㨠__proto__ ã®é¢ä¿ãå確èªãã¦ããã¨ãã§ãããJavaScriptã¯ããã ãã©ãããã£ã¦ãAltJSãªè¨èªã®ç¶æ¿ã¯ã©ããªã£ã¦ããã ããã¨æã£ã¦è©¦ããããªãã¾ããã
ã¡ãã£ã¨æ°æã¡çã«ããªãã ã宿ã«å ¥ã£ãã¿ããã«ä¿ºã¯ JavaScript ãããã£ãã£ã¦å£°é«ã«è¨ã人ãã ãããã¿ããªããããã¿ã¤ããã§ã¼ã³ã«ããç§»è²ãã¯ã©ã¹ãã¼ã¹ã®ç¶æ¿ã«æç¸ããã¦ããæèã®ãã¬ã¤ã¯ã¹ã«ã¼ãªãã ããã¿ããã«è¨ããã ãã©ããã£ãããããã©ãã»ã©ç´ æ´ããããã®ãªãã ããã¨èãã¦ãã¾ã...
ãããããã¿ã¤ããã§ã¼ã³ããããªã«ç´ æ´ãããããªããnpm ã«ããã»ã¨ãã©ã®ã©ã¤ãã©ãªããã§ã¼ã³ãæ´»ããã¦ä½ããã¦ãªãçç±ã説æã§ããªãããããããããä»çµã¿ããããé¢ç½ãã¦ããå®éã®ã¡ã³ã¿ã«ã¢ãã«ã«ãã£ãããããå®ã¯ä½¿ãç©ã«ãªããªãããããªãã ---- ã¨çã£ã¦ã¿ãã¨ããããèããªããã¦ã¿ã¾ããã
TypeScript ã§ç¶æ¿ããããããªãã¾ããã
class A {
public p:number;
public q:number;
constructor(n:number) {
this.p = n;
this.q = n * 10;
}
}
class B extends A {
constructor(n:number) {
super(n);
this.p = n * 2;
// this.q = n * 20;
}
}
var b = new B(1);
console.log(b.p, b.q);
â
var __extends = this.__extends || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } __.prototype = b.prototype; d.prototype = new __(); }; var A = (function () { function A(n) { this.p = n; this.q = n * 10; } return A; })(); var B = (function (_super) { __extends(B, _super); function B(n) { _super.call(this, n); this.p = n * 2; // this.q = n * 20; } return B; })(A); var b = new B(1); console.log(b.p, b.q);
ãªãã ãã® __extends ã£ã¦ãTypeScript ã¯å
ã®ã³ã¼ãã¨ããã¨å¯¾å¿ãã JavaScript ãåãã£ã¦ãã¨ã«ãªã£ã¦ãªãã£ãã£ã? ãªãã§å
ã®ã³ã¼ãã¨å¯¾å¿ããªããããªé¢æ°ãä½ããªãããããªããã ããã
CoffeeScript ã§ã¯ã©ãããª?
class A
constructor: (n)->
@p = n
@q = n * 10
class B extends A
constructor: (n)->
super(n)
@p = n * 2
# @q = n * 20
b = new B(1);
console.log b.p, b.q
â
var A, B, b, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; A = (function() { function A(n) { this.p = n; this.q = n * 10; } return A; })(); B = (function(_super) { __extends(B, _super); function B(n) { B.__super__.constructor.call(this, n); this.p = n * 2; } return B; })(A); b = new B(1); console.log(b.p, b.q);
ãã£ã±ãåããããªã®ãã§ããã親ã®ã¯ã©ã¹ãåç §ããæ¹æ³ãåçãéçãéãã ãã§ãã»ã¨ãã©åããã¨ãã£ã¦ãã
ã¡ãã£ã¨ã¾ã£ã¦ãJavaScript ã®ãããã¿ã¤ããã§ã¼ã³ã®æç§æ¸ã§ãã®ãn ã¨ããåæåãã©ã¡ã¼ã¿ãæã¤ãªãã¸ã§ã¯ãã®ç¶æ¿ã¯ã©ããããµãã«å®è£
ããã£ã??
function A(n) { this.p = n; this.q = n * 10; } function B(n) { this.p = n * 2; // this.q = n * 20; } B.prototype = new A(0); // ã¨ãããããªãã¸ã§ã¯ãè¦ãããã©ãããããã¡ã var b = new B(1); console.log(b.p, b.q); console.log(b.constructor);
ãã? ä¼çµ±ç㪠JavaScript ã®æç§æ¸çãªãããã¿ã¤ããã§ã¼ã³ã ã¨ãnew A(0); ã®ã¨ãããã©ããã¦ããã¾ãæ¸ããªããããã¨ã§ (new B(1) ã®ã¨ãã«) åæåãã©ã¡ã¼ã¿ã®å¤ã決ãããã®ã«ãæåã«ä½ãä»®ã«æ±ºãã¦ãããªãã¨ãããªã?? ã§ãããã 㨠b.q ã®å¤ã¯ new B(1) ã«ãã£ã¦æ±ºå®ããããã®ã¨é£ãéã??
ãã£ã¨ä¸èªç¶ãªã®ã¯ãfunction A(n) { ... } ãã³ã¼ã«ãããã¿ã¤ãã³ã°ãB.prototype = ...; ã®æç¹ãã¤ã¾ãä½ã®å®ä½ãä½ããããã å®ç¾©ã ãããã¨ããããã§ã«ã颿°ãå¼ã°ãã¡ãã£ã¦ãããã親ã«ãªã A ãã¿ã¤ã ã¹ã¿ã³ãåå¾ããªã½ã¼ã¹ã®ããã¯ã¿ãããªè²¬åãæã£ã¦ããã...
ã¨ãããã§ãããã¿ã¤ãä¿¡è ã«ãªã£ã人ãããã®ã¯ããã¯ã©ã¹ãã¼ã¹ã¨ã¯éãã®ã ãã¯ã©ã¹ãã¼ã¹ã¨ã¯ãã£ã¦ãã¨ãªãã§ãããã¡ãã£ã¨è½ã¡çãã¦ãèãæ¹ãééã£ã¦ããã£ã¦ããåã«ãTypeScript 㨠CoffeeScript ããã£ã¦ãããã¨ã¯ä½ãªã®ããããªãã¹ãå¹³æãª JavaScript ã«æ¸ãæãã¦èãã¦ã¿ã¾ããã
function A(n) { this.p = n; this.q = n * 10; } function B(n) { A.call(this, n); // ããã§è¦ªã®åæåããã»ã¹ãå¼ã¶ this.p = n * 2; // this.q = n * 20; } (function() { var tmp = function() { // ãããã¿ã¤ãã¯ãã©ã¡ã¼ã¿ãæã£ã¦ã¯ãããªã this.constructor = B; // __proto__ã®1段éç®ã«constructorããããã£ããªãJSè¬ä»æ§ã¸ã®å¯¾ç }; // hasOwnProperty ã¯ã¡ãã岿 tmp.prototype = A.prototype; // Bã®ãããã¿ã¤ãtmpã¯Aã«ä¼¼ãå¥ã®ããã¼ãªãã¸ã§ã¯ã B.prototype = new tmp(); })(); var b = new B(1); console.log(b.p, b.q);
- B ã®ãããã¿ã¤ãã¨ã㦠new ãããã®ã¯ A ã§ã¯ãªã
- ãã㯠A ã«ä¼¼ãå¥ã®ããã¼ãªãã¸ã§ã¯ã
- ãããã¿ã¤ãã®ã³ã³ã¹ãã©ã¯ã¿ã¯ãã©ã¡ã¼ã¿ãæããªã (
new A(0);ã®ä»¶) - 親ã®åæåããã»ã¹ãåæå颿°ã®ããã«å¼ã¶å¿ è¦ããã
__proto__ã®1段éç®ã«constructorããããã£ããªãJSè¬ä»æ§ã¸ã®å¯¾ç âã²ã©ã
JavaScript ã§ã¯ã©ã¹ãã¼ã¹ç¶æ¿ã§å¾ãããæ©æµã享åãããã¨æã£ãããã¦ã¼ã¶ã¼å´ã§ããã ãã®ã³ã¼ããæ¸ãå¿ è¦ããããã§ããã? ã¯ã©ã¹ãã¼ã¹ã®OOPè¨èªã§ãããã¿ã¤ããã§ã¼ã³ã®çä¼¼ããããã¨ãã¯ãé¢åã ãã©ãã¡ãã¡ã¿ã¼ã²ããã«ç§»è²ããã°ä½ã¨ããªãã¨ããã·ã³ãã«ãã ã¨ããã®ã«å¯¾ãã¦ããã¾ãã«ãã§ã¢ãããªãããããªãã§ããããã
ãã¼ãã
ãããããããã¿ã¤ããã§ã¼ã³ã®æ£ç¾©ã«æºãã¦èãã¦ã¿ã¾ããããããããã¿ã¤ããã§ã¼ã³ã¯ãä¾ãã¦ããã°ã¬ã¹ãã©ã³ã§ãç¡ææ³ãªåºé·=ãããã¿ã¤ãã¨ã®ããã¨ãããã§ããªãã£ãã¨ããã«ãææ³ã®ããåºå¡ãããå
¥ã£ã¦ããã¦ã代ããã«ãã£ã¨æå¬ã®ããæ¥å®¢ãããããã客ããã®ããããªè¦æã«çããããã¦ããããã¿ãããªãã¤ã§ããããã®å®ä½ã¯ is-a é¢ä¿ãããªãã¦ã³ã³ãã¸ã·ã§ã³ã«ããæ¡å¼µããªãã§ãç¶æ¿ã§æ¡å¼µããªããã³ã³ãã¸ã·ã§ã³ã好ãã¹ãã¨ãããã©ã¯ãã£ã¹ã«åè´ãã¦ããæ°ããã¾ãã
æ°ããã¾ã...
ã¨ããã JavaScript ã®åæè¨è¨ã«ã¯ Java ã®å§åããã£ããã¨ãå¿ãã¡ãããã¾ãããnew Hoge(); ã ã®ã³ã³ã¹ãã©ã¯ã¿ã ã®ããããããããã¯ãæ¬è³ªçã«å¿
è¦ãªãã§ãããã? ãªã«ãæ¿æ²»çãªçç±ããè¨èªã¨ãã¦ã®è¨è¨ã®å®å
¨æ§ããåªå
ããã£ã¦å¯è½æ§ã¯ãªãã§ãã?
ã§ããããã¿ã¤ããã§ã¼ã³ãããªããã£ã¡ããã£ã¦æè¿ ECMA Script 5 ã§å¢ããããã¦ãªã¤ã³ã°ã«æ¨å¥¨ãªæãã®ãã¤ãããã§ããã
Object.create - JavaScript | MDN
Object.create æ¦è¦ æå®ãããããã¿ã¤ããªãã¸ã§ã¯ãããã³ããããã£ãæã¤ãæ°ããªãªãã¸ã§ã¯ããçæãã¾ãã
function Constructor(){} o = new Constructor(); // ããã¯ä»¥ä¸ã¨åãã§ã: o = Object.create(Constructor.prototype); // ãã¡ãããå®éã«ã³ã³ã¹ãã©ã¯ã¿é¢æ°ã®åæåã³ã¼ããããå ´åã§ããObject.create ã¯ãããåæ ã§ãã¾ãã
Polyfill
if (!Object.create) { Object.create = function (o) { if (arguments.length > 1) { throw new Error('Object.create implementation only accepts the first parameter.'); } function F() {} F.prototype = o; return new F(); }; }
ãã®ä»£æ¿ã³ã¼ãã®ä¸ã§ãã£ã¦ããã¨ã¯ãAltJSç³»ãã¯ã©ã¹ãã¼ã¹ç¶æ¿ãåç¾ãããã®ãJSã«æ¸ãä¸ããã³ã¼ãã«ããã¶ãè¿ãã§ãããã³ã³ã¹ãã©ã¯ã¿ã¯ä½ã§ããããè¦ããã«ã¿ã¼ã²ãããªãã¸ã§ã¯ãã«å¤æ´å®å ¨ã©ããã¼ããã¶ããä½ããã®ãªãã¸ã§ã¯ããä½ããã°ãããã ã
...ãã£ã±ãããã¦ã¼ã¶ã¼ã³ã¼ãã«ãã³ã³ã¹ãã©ã¯ã¿ããªãã¦åå¨ãããããå¿
è¦ãªãã£ããããããECMA æ°ããå½¢ãå®ç¾©ãã¦ããå®ä½ãä½ã£ã¦ããå¿
è¦ãªãã¦ãªããã¨ããããªãå®ä½ãæã¤ãªãã¸ã§ã¯ããåå¨ãã¦ãã¦ã¼ã¶ã¼ã¯ãã®å®ä½ã«ç®ããã¶ãããªãã¸ã§ã¯ããã好ããªã¨ãã«èªç±ã«ä½ã£ã¦ãã(=ç®)ããã³ã¬ã¼ã·ã§ã³ã§ããããã³ã³ã¹ãã©ã¯ã¿é¢æ°ã®å®ç¾©ãªãã¦è¦ããªãããã¨ããããããã§OKã ããã§ãããJava ã«è¨ãããã¦ãã ããªãã§ã㣠new ã constructor ããªãã£ããã¨ã«ãã¦ãã ãããã¨...?
ãããF.prototype = o; ã® o 㯠Constructor.prototype ã§ãããã¯ã©ã¹ãã¼ã¹åç¾ã®ã»ãã§ã¯ãtmp.prototype = A.prototype; ã«å¯¾å¿ãã¾ããããªãã¸ã§ã¯ãç´æ¥ã§ã¯ãªããããã¾ã§ãåããã£ã¦ããã®åã«ãããã¿ã¤ããªãã¸ã§ã¯ããåå¨ããã¨ããèãã
ãããã¿ã¤ããã§ã¼ã³ã®ä¿¡å¥è ã¯ããªã¢ã«ãªå¤(å®éã«æ§é ãã©ããªã£ã¦ããäºæ¸¬ä¸è½)ãå ã«ãã£ã¦ããã¦ãããã©ãããããã¨ããçºæ³ãªãã ãã©ãããã©ããªãã§ãã? å¶éãããå(æ§é ã¯æ³å®ã®ç¯å²å )ãå ã«ãã£ã¦ããããã®é¢ä¿ã¨ããæ½è±¡æ¦å¿µã«å¯¾ãã¦å¤ãæå ¥ãããã¨ããçºæ³ã®ã»ãããã½ããã¦ã§ã¢è¨è¨ã¨ãã¦å¥å ¨ãªã¢ã¤ãã¢ãªããããªããã¨ãèªåãªãèãã¾ããã©ã...
ãããã«ããã§ã¼ã³ã®å
ã®å¤ã颿°ã§ãã£ã¦ããèªåãæã£ã¦ããããããã£ã®ããã« this.foo ã¨ããã ãã§èªã¿åãã¢ã¯ã»ã¹ã§ããã®ã¯æ¥½ã§ããããã¦ãèªã¿åãã¢ã¯ã»ã¹ããããæ¸ãè¾¼ã¿ã¢ã¯ã»ã¹ this.foo *= 2; ãªã©ããã¨ãå¥ã®å®ä½ãã§ãã¦ãã¾ãã¾ããããã§ãã¾ããããèªãã¨ãããã©ã¯ãããã¿ã¤ãã®ã»ãã«ã¢ã¯ã»ã¹ããªãããã¼ã¨ãããã£ã¦æ¬å½ã«ç§»è²ã£ã¦è¨ãã?
ãã£ã½ããæç¤ºçãªç§»è²ã«ã¯ä½ã®åé¡ããªãã£ãããã»ã©é¢åã§ããªãããæå³ããªãæä½ã¯ã¿ã¼ã²ããã«å±ããªãã
class Operator
constructor: (@target)->
exec: ->
@target.foo *= 2
data = { foo: 1, bar: 10 }
op = new Operator data
op.exec()
console.log data.foo # =>2
ãã¼ã¨ããããã㦠JavaScript ã®ãããã¿ã¤ããã§ã¼ã³ã¯ããªãã¸ã§ã¯ãéã®ç§»è²ãããã¢ã«æ©è½ãã¦ããªã?
ã¤ã¨ã¹!
ã¦ã¼ã¶ã¼ã¬ãã«ã§ä½¿ãããããã¿ã¤ããã§ã¼ã³ã¨ã¯ãã³ãã¼ã³ã¹ããã±ãã£ãã¯ãã¼ã³ã¨ããç¨éãé¤ãã¦ããããããå®ç¾©ãããã¡ã½ããã®ã»ãããå¼ãç¶ãããã§ããªããåä¸é¸æã®ããã¯ã¹ã¤ã³ã§ãããªãã®ã§ãã¼ãã
ãã...? ãã¼ã£ã¨ã _.extend(targetObject, myMethodSet) ã£ã¨...
// Extend a given object with all the properties in passed-in object(s). _.extend = function(obj) { if (!_.isObject(obj)) return obj; _.each(slice.call(arguments, 1), function(source) { for (var prop in source) { obj[prop] = source[prop]; } }); return obj; };
ã©ãèãã¦ããã£ã¡ã®ã»ããããã§ãããä½åã§ããä½åã§ããããããããããã Underscore ã lodash ã大人æ°ãªããã ã
ã¦ã¼ã¶ã¼ã®æ¬é³ããããªããããããã使ãããããªãã夿®µãªç¶æ¿ãªãããªãã¦ãããããä»ã«ä½¿ããéå ·ã¯ååæã£ã¦ããããªãã¦ãææ ¢ã§ããã使ããã¨ãã¦ãããããªããæåºãããéã«çç£æ§ãè½ã¡ãã
ãããã¦ãJavaScript ã¯ã¯ã½ã®å½¹ã«ãç«ããªããããã¿ã¤ããã§ã¼ã³ãå°å°ãããã¹ã¦é¢æ°ã¨åä¸é層ãªãã¸ã§ã¯ãã§èªããä¸çã§äºå®èª¿åãè¿ãã¾ããã¨ãã
ãã£ã½ãã§ããã®é ããããããã¿ã¤ãã®ç§è¡ãå©ç¨ãã¦ã³ã³ãã¤ã©ãä½ããããããã¨ãéªæã§ããã¯ã©ã¹ãã¼ã¹ãªãã¸ã§ã¯ãæåã JavaScript å ã«åç¾ãããã¨ããã®ã AltJSãã¨ããä¸çã®æµããªããã§ãã
ããã¼ãããèãããããªãã§ããããã©ãèãã¦ããnpm è¦ã¦ããAltJS è¨èªè¦ã¦ãã客観çã«ã(èªåã ã£ã¦ãããªçµæ«ã¯æå¾ ãã¦ãªãã£ããã©)
ææ³
ãããã¿ã¤ããã§ã¼ã³ã¨ãããããã¿ãã®é åã«ããããã¨ãªããç¾å®ä¸çã§ã¡ã³ã¿ã«ã¢ãã«ã«ãã£ãããããã¤ãå®éã«ä½¿ããæè¡ã¨ã¯ä½ããã¨ãããã¨ãè¦å¤±ã£ã¦ã¯ãããªãã¨æãã¾ããããã¨ããã£ã±ã JavaScript 㯠Java ã®æ¿æ²»å§åãåãã¦ã ã¾ãã ã¾ããªãªã¼ã¹ãããããã©ã¦ã¶ãã¯ãã®ããã®ä¸éå端ãªãã¶ã¤ã³ã®è¨èªãªã®ã ã¨ããã®ã¯ããã¾ã ã«éè¦ãªè¦ç¹ãªãã ãªã¨æãåºãç´ãã¾ããã
ã¦ã£ã¼ã³ ã¦ã£ã¼ã³ ããµã«ãªé²å£å±éä¸...