[過去ログ] + JavaScript(ECMAScript)質問用スレッド vol.122 + [無断転載禁止]©2ch.net (1002レス)
前次1-
抽出解除 必死チェッカー(本家) (べ) 自ID レス栞 あぼーん

このスレッドは過去ログ倉庫に格納されています。
次スレ検索 歴削→次スレ 栞削→次スレ 過去ログメニュー
396: デフォルトの名無しさん [sage] 2016/09/09(金) 16:45:04.06 ID:dcApvFQ/(1/5) AAS
lengthって名前の関数を作るときなんですが、

var Sample = function() {
      this.length = function() {
          return 100;
      }
};
Sample.length = function() {
    return 100;
};

let sample = new Sample();
console.log(sample.length()); // これは OK
console.log(Sample.length()); // これは エラー

っていう動作になるのはこういう仕様でしょうか?
397: デフォルトの名無しさん [sage] 2016/09/09(金) 16:46:02.70 ID:dcApvFQ/(2/5) AAS
すみません、 使えなかった
こちらです。

var Sample = function() {
  this.length = function() {
    return 100;
  }
};
Sample.length = function() {
  return 100;
};

let sample = new Sample();
console.log(sample.length());
console.log(Sample.length());
399
(1): デフォルトの名無しさん [sage] 2016/09/09(金) 18:03:02.38 ID:dcApvFQ/(3/5) AAS
>>398
398(1): デフォルトの名無しさん [sage] 2016/09/09(金) 17:42:46.36 ID:mXWYcTT5(1) AAS
何がしたいの?w
具体的には
VectorUtil.length = function(x, y, z) {
  return Math.sqrt(x*x + y*y + z*z);
};
これがしたかったです
403
(1): デフォルトの名無しさん [sage] 2016/09/09(金) 21:52:24.77 ID:dcApvFQ/(4/5) AAS
>>400
400(1): デフォルトの名無しさん [sage] 2016/09/09(金) 19:06:00.54 ID:R/34hIbI(1) AAS
Function.lengthは引数の数で定義済なんで無いの
そういう仕様っぽいような気がしました。
なぜ上書きできないのか、わかってないですが。

>>401
401(1): デフォルトの名無しさん [sage] 2016/09/09(金) 19:15:39.77 ID:4wEFuM4p(2/3) AAS
>>399
class構文を使えばできる。
class Sample {
static length() { return 100 }
}
Sample.length()
なるほど、ありがとうございます。
classって糖衣構文だと思ってるんですが
内部的にはどうやってるのでしょう(調べてみます)
406: デフォルトの名無しさん [sage] 2016/09/09(金) 22:47:46.14 ID:dcApvFQ/(5/5) AAS
>>404
404(1): デフォルトの名無しさん [sage] 2016/09/09(金) 22:11:57.62 ID:4wEFuM4p(3/3) AAS
>>なぜ上書きできないのか
[[Writable]]: false だから
外部リンク:tc39.github.io
ただし[[Configurable]]: true なのでディスクリプタセット系メソッドで上書きはできる

>>内部的にはどうやってるのでしょう
コンストラクタが定義され、その時点では上記のようにlengthプロパティを持っているが、
その後PropertyDefinitionEvaluation→ディスクリプタセット系によって上書きされる
外部リンク:tc39.github.io
>>405
405(1): デフォルトの名無しさん [sage] 2016/09/09(金) 22:27:00.10 ID:Ml2lLB3N(1) AAS
>>403
babelで変換したらこうなったよ。
Object.definePropertyが鍵のようだ。
"use strict";
var _createClass = function () {
 function defineProperties(target, props) {
  for (var i = 0; i < props.length; i++) {
   var descriptor = props[i];
   descriptor.enumerable = descriptor.enumerable || false;
   descriptor.configurable = true;
   if ("value" in descriptor) descriptor.writable = true;
   Object.defineProperty(target, descriptor.key, descriptor);
  }
 }
 return function (Constructor, protoProps, staticProps) {
  if (protoProps) defineProperties(Constructor.prototype, protoProps);
  if (staticProps) defineProperties(Constructor, staticProps); return Constructor;
 };
}();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Sample = function () {
function Sample() {
_classCallCheck(this, Sample);
}
_createClass(Sample, null, [{
key: "length",
value: function length() {
return 100;
}
}]);
return Sample;
}();
ありがとうございます。
勉強になりました。
前次1-
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル

ぬこの手 ぬこTOP 0.052s