当我this使用apply然后设置为字符串时,我发现了有趣的输出console.log.这是怎么回事?
在Chrome的Javascript控制台中,
(function(){ return this }).apply("hello");
Run Code Online (Sandbox Code Playgroud)
输出到:
String {0: "h", 1: "e", 2: "l", 3: "l", 4: "o"}
Run Code Online (Sandbox Code Playgroud)
为什么"hello"不像我预期的那样?
有趣的是,检查此输出typeof:
typeof (function(){ return this }).apply("hello");
Run Code Online (Sandbox Code Playgroud)
给了我"object",而不是"string".
我猜这是一些apply我不明白的巫术?
我正在寻找使用Mcrypt的简单但加密强大的AES实现AES.
希望将其归结为一对简单的功能,$garble = encrypt($key, $payload)以及$payload = decrypt($key, $garble).
假设我已经在使用Web MIDI API在MIDI输入上侦听消息,现在我正在尝试理解和利用我接收的数据。
如何从中解析一些基本信息MIDIMessageEvent?
我如何解释一些基本的MIDI事件的解析信息?
给定以下示例,我很惊讶地发现,添加font-size: 1em到<button>元素会导致内容大小增加
<p>normal</p>
<p style="font-size: 1em;">normal</p>
<button>normal</button>
<button style="font-size: 1em;">big?</button>Run Code Online (Sandbox Code Playgroud)
我期望两个按钮的大小一样,如段落所示。为什么一个按钮比另一个大?
TL; DR? 为什么我不能在构造函数中覆盖构造函数的原型?
我正在弄清楚我的原型继承模式.我不喜欢原型通常是从构造函数外部定义的,并希望逻辑上更好地封装事物.
我发现我期望工作的一条神奇的线路没有.
function Orifice(){
this.exhaust=function(){};
this.ingest=function(){};
}
var standardOrifice = new Orifice();
function Sphincter(){
this.constructor.prototype = standardOrifice; // <-- does not work
this.relax=function(){};
this.tighten=function(){};
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,我可以编写单独的属性this.constructor.prototype,但是我不能像构造函数定义之外那样覆盖整个原型对象.
所以像这样的东西工作:
this.constructor.prototype.exhaust = standardOrifice.exhaust;
this.constructor.prototype.ingest = standardOrifice.ingest;
Run Code Online (Sandbox Code Playgroud)
为此我可以创建一个简单的克隆函数来处理这个:
function extend(target){
return {
from: function(obj){
target.__proto__ = obj.constructor.prototype;
for (key in obj) if (obj.hasOwnProperty(key)) target[key]=obj[key];
return target;
}
};
}
Run Code Online (Sandbox Code Playgroud)
值得庆幸的是,到目前为止,在我的测试中,这种技术似乎运行良好,但我不确定是否存在我可能遗漏的细节或性能案例.
function Sphincter(){
extend(this.constructor.prototype).from(standardOrifice);
//...
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能在构造函数中覆盖构造函数的原型?但我可以在构造函数之外?单独编写属性是否在构造函数中起作用?
javascript ×3
aes ×1
cryptography ×1
css ×1
ecmascript-6 ×1
encryption ×1
html ×1
mcrypt ×1
midi ×1
php ×1
web-midi ×1