实现接口和对象(Implementing Interfaces and Objects)
当一个类实现一个接口时,是否使得从该类实例化的对象被视为该接口的对象?
即在一个实现Runnable接口的类上,是否使得从该类创建的实例被称为Runnable对象?
因此,在期望Runnable对象引用变量的地方(比如,在方法或构造函数的参数中),为什么我们可以提供该类的实例作为该方法或构造函数的参数是合法的? 是否因为通过实现接口,类本质上是接口的一个对象?
When a class implements an interface, does that make objects instantiated from the class be perceived as an object of that Interface?
i.e. Upon a class implementing the Runnable interface, does that make instances created from that class to be called a Runnable object?
So, where a Runnable object reference variable is expected (say, in the parameter of a method or a constructor), why is it legal that we can provide an instance of the class as an argument to that method or constructor? Is it because by implementing the interface, the class, is in essence, an object of the Interface?
原文:https://stackoverflow.com/questions/29860272
最满意答案
正如你刚才提到的,es5-shim在浏览器的JavaScript引擎不兼容ES5的情况下增加了一些JavaScript函数,比如map,reduce,some和forEach。 Underscore.js在“_”命名空间中添加了类似于上述(和更多)的实用程序方法。 所以,在这种情况下,在同一个应用程序中使用这两个都是多余的。
也就是说,es5-shim增加了一些功能,比如Date.now,Date.toJSON,Function.bind,String.trim(以及更多),它们在Underscore.js中没有直接的等价物。 例如Underscore提供的_.bind和_.bindAll等同于Function.bind,但Underscore.js不提供Date.now和Date.toJSON。
因此,如果您使用es5-shim提供的其他方法,则可以在应用程序中使用es5-shim和Underscore.js。 但是,如果您不使用这些额外的方法,那么使用es5-shim只会是应用程序大小的不必要的增加和带宽的浪费。
es5-shim, as you rightly mentioned, adds some JavaScript functions like map, reduce, some and forEach on Array if the browser's JavaScript engine is not ES5 compatible. Underscore.js adds utility methods similar to the above (and more) within the "_" namespace. So, in this case, using both in the same application is redundant.
That said, es5-shim, adds a few more functions like Date.now, Date.toJSON, Function.bind, String.trim (and more) some which don't have direct equivalents in Underscore.js. For example Underscore does provide _.bind and _.bindAll that is equivalent to Function.bind, but Underscore.js does not provide Date.now and Date.toJSON.
So, if you use these additional methods provided by es5-shim you can stay with both es5-shim and Underscore.js in your application. But, if you are not using these additional methods, then using es5-shim would just be an unnecessary increase in application size and waste of bandwidth.
相关问答
更多-
underscore.js每个函数(underscore.js each function)[2022-02-10]
发生这种情况是因为范围在循环内部发生了变化,您可以通过使用绑定功能将您的函数与this绑定来保持相同的范围。 bind()方法创建一个新的函数,该函数在调用时将其关键字设置为提供的值,并在调用新函数时提供的任何前面给定的参数序列。 代码示例: _.each(this.selectedCharges, function(selectedCharge, key){ if(selectedCharge._id == charge._id){ this.selectedCharges.spl ... -
Underscore.js分组对象(Underscore.js Grouping Object)[2022-12-30]
_.map(_.groupBy(arr, 'menu'), function(roles, menu) { var entry = {menu: menu}; _.each(roles, function(role) { entry[role.role] = "OK"; }); return entry; }) 我假设你的输出是: [ { "menu": "Setting", "admin": "OK", "user": "OK", "developer": "OK" }, { " ... -
针对ngRoute和Underscore.js的RequireJS shim配置(RequireJS shim config for ngRoute and Underscore.js)[2023-07-15]
您的代码存在多个问题: 除非你使用古老版本的jQuery,否则jQuery不需要shim 。 下划线也是如此。 你必须在所有小写字母中引用jQuery作为模块: jquery 。 它仍然在全局空间中导出jQuery ,但模块名称(放在传递给require或define依赖列表中)必须全部小写。 您不能在shim复制条目。 你有这个例子: angularRoute: { exports: "ngRoute" }, angularRoute: { deps: ['angular'] }, 只有第二 ... -
我们试试这个: $(".info:contains('...')").hide(); 请让我知道结果。 谢谢。 Let's try this: $(".info:contains('...')").hide(); Please let me know the result. Thanks.
-
Underscore的模板(故意)简单且极小,因此没有任何内置的格式化实用程序。 但是,您可以在<%= ... %>放置您想要的任何JavaScript表达式,以便您可以轻松添加自己的格式设置实用程序。 您可以在JavaScript中执行以下操作: window.fmt = { iso_date: function(d) { // Your favorite ISO 8601 date formatter goes here, this // is just a qu ...
-
你会想要使用JavaScript版本。 CoffeeScript版本可能只是作者在使用CoffeeScript进行的,因为他是CoffeeScript和Underscore的作者。 另外,CoffeeScript版本引入了一个编译步骤(假设您在浏览器中使用它,而不是在具有node.js的服务器上)。 作为另一种选择,检查Lodash。 它是Underscore的下架替代品,并且由于许多原因是更好的选择。 它在过去几天刚刚发布了v1.0。 You'll want to use the JavaScript v ...
-
你在这里检查了下划线测试套件吗? Have you checked Underscore Test suite here.
-
正如你刚才提到的,es5-shim在浏览器的JavaScript引擎不兼容ES5的情况下增加了一些JavaScript函数,比如map,reduce,some和forEach。 Underscore.js在“_”命名空间中添加了类似于上述(和更多)的实用程序方法。 所以,在这种情况下,在同一个应用程序中使用这两个都是多余的。 也就是说,es5-shim增加了一些功能,比如Date.now,Date.toJSON,Function.bind,String.trim(以及更多),它们在Underscore.js ...
-
定义了一个全局exports对象,它阻止了underscore.js的正确安装。 该对象声明了两个函数: compile()和compileToString() 。 函数的实现揭示了LF对象,它最有可能属于Disqus Javascript API: http : //www.livefyre.com/docs/javascript-sdk-reference 。 这是对不成功的underscore.js安装的调查。 仍然困惑于我现在该怎么做以及如何正确解决它: underscore.js,名为“expor ...
-
es5-shim或chrome框架,用于使角度应用程序在IE中运行(es5-shim or chrome frame for making angular apps work in IE)[2023-06-25]
谷歌Chrome Frame是一个应用程序,需要用户安装它,es5-shim是JavaScript代码,你只需要包含它。 如果你想在IE中使用AngularJS,你应该阅读: http : //docs.angularjs.org/guide/ie 。 简而言之,包括es5-shim(可选但非常有用)和JSON polyfill。 Google Chrome Frame is a application, and need user to install it, es5-shim is JavaScript ...