这个函数的参数在哪里?
where do the arguments go in this function?
我正在研究并试图理解 https://trianglify.io/ which can be found at https://github.com/qrohlf/trianglify 的源代码。在 lib/trianglify.js 文件中,我遇到了以下代码行:
var x_color = chroma.scale(opts.x_colors).mode(opts.color_space);
var y_color = chroma.scale(opts.y_colors).mode(opts.color_space);
gradient = function(x, y) {
return chroma.interpolate(x_color(x), y_color(y), 0.5, opts.color_space);
};
我的问题是当 x_color(x) 被调用时,"x" 参数去哪里了?如果这个参数没有出现在定义中,它如何传递给函数?我这样做的主要目的是向 x_color() 添加一些额外的自定义参数,但如果我不知道参数在函数中是如何处理的,我就不能这样做。
编辑
.mode(opts.color_space)函数可以在https://github.com/gka/chroma.js/blob/master/src/scale.coffee第158行找到,内容如下:
f.mode = (_m) ->
if not arguments.length
return _mode
_mode = _m
resetCache()
f
由于我的 coffeescript 知识有限,所以不确定该怎么做。
色度是 chroma.js
的一部分。
查看代码,chroma.scale(...)
构造了一个函数,其原型为fluent methods。
f = function(v) {
var c;
c = chroma(getColor(v));
if (_out && c[_out]) {
return c[_out]();
} else {
return c;
}
};
f.mode = function(_m) {
if (!arguments.length) {
return _mode;
}
_mode = _m;
resetCache();
return f;
};
因此,当您调用 chroma.scale(...)
它 returns f 时,然后当您调用 .mode(...)
返回的对象时,它再次 returns 相同的实例 f.
f的实例是通过以下方法创建的:
chroma = function() {
if (arguments[0] instanceof Color) {
return arguments[0];
}
return (function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args);
return Object(result) === result ? result : child;
})(Color, arguments, function(){});
};
如您所见,这使用了 arguments 对象。 Mozilla 将参数对象定义为:
The arguments object is an Array-like object corresponding to the arguments passed to a function.
简而言之,即使你没有在函数签名中指定参数名称,arguments对象仍然存在,你传递的任何参数都将存在于arguments数组中。
我创建了一个使用参数数组的示例 here
function a() {
alert(arguments[0] + ' is ' + arguments[1] + ' years old.');
}
function b(name) {
return function test() {
alert(name + ' will vw ' + arguments[0] + ' years old on his next birthday.');
}
}
a('John', 29);
var example2 = b('John');
example2(30);
在第一个示例中,有一个直接的函数调用,但在第二个示例中,我从 b()
方法返回一个实际函数,然后调用该返回函数。
我正在研究并试图理解 https://trianglify.io/ which can be found at https://github.com/qrohlf/trianglify 的源代码。在 lib/trianglify.js 文件中,我遇到了以下代码行:
var x_color = chroma.scale(opts.x_colors).mode(opts.color_space);
var y_color = chroma.scale(opts.y_colors).mode(opts.color_space);
gradient = function(x, y) {
return chroma.interpolate(x_color(x), y_color(y), 0.5, opts.color_space);
};
我的问题是当 x_color(x) 被调用时,"x" 参数去哪里了?如果这个参数没有出现在定义中,它如何传递给函数?我这样做的主要目的是向 x_color() 添加一些额外的自定义参数,但如果我不知道参数在函数中是如何处理的,我就不能这样做。
编辑 .mode(opts.color_space)函数可以在https://github.com/gka/chroma.js/blob/master/src/scale.coffee第158行找到,内容如下:
f.mode = (_m) ->
if not arguments.length
return _mode
_mode = _m
resetCache()
f
由于我的 coffeescript 知识有限,所以不确定该怎么做。
色度是 chroma.js
的一部分。
查看代码,chroma.scale(...)
构造了一个函数,其原型为fluent methods。
f = function(v) {
var c;
c = chroma(getColor(v));
if (_out && c[_out]) {
return c[_out]();
} else {
return c;
}
};
f.mode = function(_m) {
if (!arguments.length) {
return _mode;
}
_mode = _m;
resetCache();
return f;
};
因此,当您调用 chroma.scale(...)
它 returns f 时,然后当您调用 .mode(...)
返回的对象时,它再次 returns 相同的实例 f.
f的实例是通过以下方法创建的:
chroma = function() {
if (arguments[0] instanceof Color) {
return arguments[0];
}
return (function(func, args, ctor) {
ctor.prototype = func.prototype;
var child = new ctor, result = func.apply(child, args);
return Object(result) === result ? result : child;
})(Color, arguments, function(){});
};
如您所见,这使用了 arguments 对象。 Mozilla 将参数对象定义为:
The arguments object is an Array-like object corresponding to the arguments passed to a function.
简而言之,即使你没有在函数签名中指定参数名称,arguments对象仍然存在,你传递的任何参数都将存在于arguments数组中。
我创建了一个使用参数数组的示例 here
function a() {
alert(arguments[0] + ' is ' + arguments[1] + ' years old.');
}
function b(name) {
return function test() {
alert(name + ' will vw ' + arguments[0] + ' years old on his next birthday.');
}
}
a('John', 29);
var example2 = b('John');
example2(30);
在第一个示例中,有一个直接的函数调用,但在第二个示例中,我从 b()
方法返回一个实际函数,然后调用该返回函数。