现代化测试
为什么这会同时发出警报,是的还是错误的?
Modernizr.load([
{
test: Modernizr.cssgradients,
yep: alert('Supports it!'),
nope: alert('Oh, damn! This browser sucks!')
}
]);
我在 OS X 上使用最新的 chrome。
How come this alerts both, yes and false?
Modernizr.load([
{
test: Modernizr.cssgradients,
yep: alert('Supports it!'),
nope: alert('Oh, damn! This browser sucks!')
}
]);
I'm using the latest chrome on OS X.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为您直接在那里调用
alert()
,并且alert()
的结果(始终undefined
)被分配给yep
和nope
属性。您需要将alert()
包装在一个函数中,然后分配该函数:这个 still 不起作用,因为它不是 yepnope 的工作方式。
yep
和nope
应该是加载的 JS 文件的路径:正如您自己发现的,如果您不想使用集成的 yepnope.js,您可以使用现代化传统方式:
Because you're calling
alert()
directly there, and the result fromalert()
(alwaysundefined
) is assigned to theyep
andnope
properties. You need to wrapalert()
in a function and assign that function instead:This still won't work because it's not how yepnope works.
yep
andnope
should be paths to JS files that are loaded:As you discovered yourself, if you don't want to use the integrated yepnope.js, you can just use Modernizr the traditional way:
使用 yepnope 前缀,可以运行预定义的命名函数。注意:我只在 OS X 上使用最新的 chrome 对此进行了测试。
但是,要使其工作,您将需要一个“虚拟 URL”,例如您计划在页面上加载的图像(您的徽标是一个很好的候选者) 。
此外,由于
Modernizr.load
仅为yepnope.apply
方法提供别名,因此您需要通过名称引用yepnope
来添加前缀。当然,如果您不想污染全局
window
命名空间,您可以将命名函数放入对象中并更改window[resourceObj.url]();
到window.MyObj[resourceObj.url]();
。其真正的力量在于回调函数触发,命名函数也可以调用 Modernizr.load ,和/或者您可以编写比通用函数更有目的的前缀此处显示的执行者。
With yepnope prefixes, it's possible to run predefined, named functions. Note: I have only tested this with latest chrome on OS X.
However, for this to work, you will need a "dummy URL", e.g., an image that you plan to load on the page (your logo is a good candidate).
Also, because
Modernizr.load
only aliases theyepnope.apply
method, you will need to refer toyepnope
by name to add a prefix.Of course, if you don't want to pollute the global
window
namespace, you could put your named functions in an object and changewindow[resourceObj.url]();
towindow.MyObj[resourceObj.url]();
.The real power of this is that
callback
functions fire, the named functions can callModernizr.load
as well, and/or you could write a more purposeful prefix than the generic function executor shown here.yep
和nope
参数不接受函数作为参数。它们应该是一个字符串
或一个字符串数组
,指示根据测试
是成功还是失败来加载的脚本。有关详细信息,请参阅Modernizr.load
上的文档。The
yep
andnope
parameters do not accept functions as arguments. They should be astring
or anarray of strings
indicating scripts to load based on whether thetest
succeeded or failed. See the documentation onModernizr.load
for more information.