关于EcmaScript-5 Function.prototype.bind实现,我有一个非常有趣的问题.通常在使用bind时,您可以这样做:
var myFunction = function() {
alert(this);
}.bind(123);
// will alert 123
myFunction();
Run Code Online (Sandbox Code Playgroud)
好的,这很酷,但是当我们这样做时会发生什么?
// rebind binded function
myFunction = myFunction.bind('foobar');
// will alert... 123!
myFunction();
Run Code Online (Sandbox Code Playgroud)
我理解,就Function.prototype.bind的实现方式而言,这是完全合乎逻辑的行为(https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind).但在现实生活中,这种行为完全没用,不是吗?问题是:是bug还是功能?如果它是一个错误,为什么它没有提到?如果它是一个功能,为什么那么具有原生"绑定"实现的谷歌浏览器的行为完全相同?
为了更清楚,我认为更有意义,这里是实现Function.prototype.bind的代码片段有点不同:
if (!Function.prototype.bind) {
Function.prototype.bind = function() {
var funcObj = this;
var original = funcObj;
var extraArgs = Array.prototype.slice.call(arguments);
var thisObj = extraArgs.shift();
var func = function() {
var thatObj = thisObj;
return original.apply(thatObj, extraArgs.concat(
Array.prototype.slice.call(
arguments, extraArgs.length
)
));
};
func.bind = function() {
var args = Array.prototype.slice.call(arguments); …Run Code Online (Sandbox Code Playgroud) 我有一个关于DFA最小化的问题.所以我使用了众所周知的技术将正则表达式转换为NFA,然后使用goto/closure算法从中构造DFA.现在问题是如何最小化它?我在这里看过关于它的文章:http://www.youtube.com/watch? v = T9Z66NF5YRk ,我仍然无法理解.什么是DFA最小化?这只是合并IDENTICAL状态(状态在相同的字符上进入相同的状态)还是不同的东西?
所以,我开始使用以下语法:
%digit = '0'..'9'
%letter = 'a'..'z' | 'A'..'Z'
%exponent = ("e" | "E") ("+" | "-")? digit+
T_INT = digit+
T_FLOAT = T_INT exponent
T_IDENTIFIER = (letter | "$" | "_") (letter | "$" | "_" | digit)*
Run Code Online (Sandbox Code Playgroud)
最终得到以下DFA(表示为JSON):
{
"START": [{
"type": "range",
"from": 36,
"to": 36,
"shift": "1"
}, {
"type": "range",
"from": 48,
"to": 57,
"shift": "2"
}, {
"type": "range",
"from": 65,
"to": 90,
"shift": "1"
}, …Run Code Online (Sandbox Code Playgroud)