简体   繁体   English

当我们不需要它们的所有功能时,为什么要下载整个JavaScript框架? 我们能建议什么? (JavaScript的)

[英]Why to download a ENTIRE JavaScript framework when we do not need all features of them? What could we suggest? (JavaScript)

I like Google Web Tookit API approach. 我喜欢Google Web Tookit API方法。 It use Java language behind the scenes that compiles ONLY JavaScript code WHOSE TARGET BROWSER NEEDS. 它在幕后使用Java语言,只编译目标浏览器所需的JavaScript代码。 It happens some developers would like to use that feature in pure JavaScript language. 有些开发人员希望在纯JavaScript语言中使用该功能。

Anwser: WHAT COULD WE SUGGEST in order to fullfill this requirement ? Anwser:为了满足这一要求,我们可以提出什么建议?

I suggest to use JavaScript comments (as a flag) as a way some compiler (like Yahoo JavaScript compiler) analises our app JavaScript code and generates only a JavaScript Framework code needed. 我建议使用JavaScript注释(作为标志)作为某种编译器(如Yahoo JavaScript编译器)分析我们的应用程序JavaScript代码并仅生成所需的JavaScript框架代码的方式。

Example: a hypothetical JavaScript framework (JQuery, Mootools, Prototype ect) code 示例:假设的JavaScript框架(JQuery,Mootools,Prototype等)代码

// depends function say
funcion sayHello() {
    // some code   
}

function say() {
   // some code
}

// more and more no needed Javascript framework functions in our app

So when my app use a function sayHello, only that sayHello function and its dependencies would be filtered through JavaScript comments, nothing else. 因此,当我的应用程序使用函数sayHello时,只有sayHello函数及其依赖项将通过JavaScript注释进行过滤,没有别的。 So, this way our application would be lighter, by using only JavaScript Framework code needed. 因此,通过仅使用所需的JavaScript Framework代码,我们的应用程序将更轻松。

And you: what do you suggest ? 而你:你有什么建议?

If the JavaScript code of the framework is served as a cacheable file then the download cost of requesting the entire framework (eg jQuery.js) can be eliminated, but if you were generating the framework code on the fly (as you suggest above) then it's going to be harder to take advantage of caching. 如果框架的JavaScript代码作为可缓存文件提供,则可以消除请求整个框架(例如jQuery.js)的下载成本,但是如果您在运行中生成框架代码(如上所述),那么利用缓存会变得更加困难。

On top of this the memory cost of defining the entire framework is probably unlikely to be problematic (assuming the framework is written sensibly). 除此之外,定义整个框架的内存成本可能不太可能成问题(假设框架是合理编写的)。

So, pulling in the entire framework, as is the common case, is simple, works well and doesn't require a particular server-side infrastructure (like GWT does). 因此,通常情况下,拉入整个框架很简单,运行良好,不需要特定的服务器端基础结构(如GWT)。

There's one case where it would make sense to me--a jQuery for iPhone. 有一种情况对我有用 - 一款适用于iPhone的jQuery。 It doesn't make any sense for all the non-Safari baggage in jQuery to be in there slowing things down. 对于jQuery中的所有非Safari行李来说,放慢速度是没有任何意义的。

For the typical desktop, though, if you get a JS library from the Google APIs, it's likely already cached and ready to go. 但是对于典型的桌面,如果从Google API获得JS库,它可能已经缓存并准备好了。

I would suggest learning to program in JavaScript and understanding the various peculiarities of the different DOM implementations, then writing just the code necessary for your application. 我建议学习用JavaScript编程并理解不同DOM实现的各种特性,然后只编写应用程序所需的代码。 If you really don't want to have to deal with re-creating all the event handling shenanigans and so on, then nick the relevant techniques from the libraries. 如果你真的不想处理重新创建所有事件处理恶作剧等等,那么从库中删除相关技术。 You'll learn a lot more about what you're doing that way, as you'll need to actually understand how it all works to be able to integrate those techniques with your application. 您将更多地了解您正在以这种方式进行的操作,因为您需要真正了解如何将这些技术与您的应用程序集成。

Having worked professionally with JavaScript since 1996 I, like many, was initially tempted by the apparent ease of use offered by libraries; 自1996年以来一直专注于JavaScript,我和许多人一样,最初受到图书馆提供的明显易用性的诱惑; but if I see one more answer on here that says "use jQuery" (followed by some code that isn't even optimal in jQuery) when the correct answer is to use an existing and well-documented feature of JavaScript that works on every single implementation since Netscape Navigator 3, I'll scream ;-) 但是如果我在这里看到另外一个回答“使用jQuery”(后面跟着一些jQuery中甚至不是最优的代码),那么正确的答案是使用现有的,记录良好的JavaScript功能,这个功能适用于每一个从Netscape Navigator 3开始实施,我会尖叫;-)

I don't think it would be worth it to add the complexity of selected inclusion. 我不认为增加选定包含的复杂性是值得的。 jQuery is 57k and loads very fast. jQuery是57k并且加载速度非常快。 Your energy is much better spent worrying about other things that might actually matter. 在担心其他可能重要的事情时,你的精力要好得多。

I'm currently doing something like this with my javascript code base, but only because I'm building a very large single-page ajax application. 我目前正在用我的javascript代码库做这样的事情,但仅仅是因为我正在构建一个非常大的单页面ajax应用程序。 In comments at the top of the page I do java style import statements to include other javascript "classes"/objects. 在页面顶部的注释中,我使用java样式的import语句来包含其他javascript“类”/对象。 This is then used to create a single javascript file from the dozens of files in the application, pulling in only the code that is needed. 然后,它用于从应用程序中的许多文件创建单个javascript文件,仅提取所需的代码。

That said, it's not being done to reduce a small library like jquery into an even smaller library for just the things needed. 也就是说,将jquery这样的小型库简化为一个更小的库只是为了满足所需要的东西。 When it comes to that stuff, I think caching or a CDN should probably be good enough. 谈到那些东西,我认为缓存或CDN 可能应该足够好。

我不了解其他框架,但是使用Dojo Toolkit,您可以通过在AOL或Google中使用您网站中的源的公共CDN副本来更好地利用缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我们需要javascript mvc? - Why do we need javascript mvc? 在Play框架中使用javascript路由时,为什么还要在路由文件中需要映射呢? - Why do we bother needing the mapping in route file when we use javascript routing in Play framework? 我们为什么需要javascript:在进行内联javascript调用时 - why do we need javascript: while making inline javascript calls javascript (ES6):为什么我们需要集合? - javascript (ES6): Why do we need sets? 为什么我们需要在JavaScript继承中调用父构造函数 - why do we need to call the parent constructor in JavaScript Inheritance 为什么我们需要JavaScript中的自引用对象 - Why do we need self referential object in javascript 为什么我们需要IIFE在Javascript中进行模块作用域定义? - Why do we need IIFE's to have module scoping in Javascript? 为什么我们在Javascript中的类中需要“var self = this”? - Why do we need “var self = this” in classes in Javascript? 为什么我们需要将Firefox驱动程序强制转换为javascript执行程序? - Why do we need to typecast Firefox driver to javascript executor? 为什么我们需要VariableEnvironment来识别Javascript中一个执行上下文的state? - Why do we need VariableEnvironment to identify the state of an Execution Context in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM