简体   繁体   English

为什么JavaScript有这么令人困惑的API?

[英]Why does JavaScript have such a confusing API?

I was wondering why JavaScript has such a confusing API. 我想知道为什么JavaScript有这么令人困惑的API。 It is not consistent across browsers, there is a different way to get the value from each type of form input, and it is unforgiving of mistakes. 它在浏览器之间并不一致,从每种类型的表单输入中获取值有不同的方法,并且它对错误是无法容忍的。 jQuery has changed my life because its API is so much easier. jQuery改变了我的生活,因为它的API非常简单。 Why did the creators of JavaScript not set up the API like that in the first place? 为什么JavaScript的创建者没有首先像这样设置API?

The JavaScript API, itself, is consistent between browsers (and is defined by ECMA, though originally developed by Netscape). JavaScript API本身在浏览器之间是一致的(并且由ECMA定义,尽管最初由Netscape开发)。 The difference between browsers is the document object model (DOM). 浏览器之间的区别是文档对象模型(DOM)。 The DOM was developed independently by the different browsers, originally IE and Netscape, but now IE, Mozilla and others. DOM是由不同的浏览器独立开发的,最初是IE和Netscape,但现在是IE,Mozilla等。 The W3C has joined to try to consolidate the differences and create a common standard. W3C已加入尝试巩固差异并创建共同标准。 For backward compatibility, the old differences remain. 为了向后兼容,旧的差异仍然存在。 And, yes, jQuery has gone a long way toward making the DOM easier. 而且,是的,jQuery在使DOM变得更容易方面走了很长的路。

Creators of Javascript did not setup the API, since Javascript is a language, not an API. Javascript的创建者没有设置API,因为Javascript是一种语言,而不是API。

What you are refering to is the Document Object Model (DOM) which is the document manipulation API. 您所指的是文档对象模型(DOM),它是文档操作API。 It is a standard specified by the W3C and its behaviour should be consistent among browsers. 它是W3C指定的标准,其行为应该在浏览器之间保持一致。

Unfortunately, some parts were badly specified, some other parts are badly implemented by browser vendors. 不幸的是,有些部分指定不当,浏览器供应商严重执行其他一些部分。 Additionally, vendors extend this API with proprietary extensions that may never be added in the standard but that are very popular (like document.all in its time). 此外,供应商使用专有扩展来扩展此API,这些扩展可能永远不会添加到标准中,但非常受欢迎(如当时的document.all)。

That's why today's API in browsers are so inconsistent. 这就是为什么今天的浏览器API如此不一致。

I think most of it is the remnant of the browser war. 我认为大多数是浏览器战争的残余。 Javascript had a very troublesome history, made of a total war between microsoft and netscape, with Sun involved as well. Javascript有一个非常麻烦的历史,由微软和网景之间的全面战争组成,Sun也参与其中。 Javascript is actually a very nice language. Javascript实际上是一种非常好的语言。 It has some critical design mistakes, but you can work around them. 它有一些关键的设计错误,但你可以解决它们。 As for the API, you can use a good wrapping library that hides all the complexity and uses the most appropriate API. 至于API,您可以使用一个好的包装库来隐藏所有复杂性并使用最合适的API。

One important suggestion, if I may. 一个重要的建议,如果可以的话。 Don't fight it, nor try to use it masked as something else. 不要打它,也不要试图掩盖它作为别的东西。 Embrace it even with its defects. 即使有缺陷也要拥抱它。 Once you know them, you won't step on them anymore, or if something is fishy you will find the problem easily. 一旦你了解它们,你就不会再踩到它们,或者如果有些东西可疑,你会很容易发现问题。

I'll bite. 我会咬人的。 Check out Douglas Crockford's videos ( http://javascript.crockford.com/ ), he does a good job explaining why some of JavaScript is in the situation that it's in. ( http://yuiblog.com/blog/2007/01/24/video-crockford-tjpl/ ) 看看道格拉斯·克罗克福德的视频( http://javascript.crockford.com/ ),他很好地解释了为什么一些JavaScript处于它所处的状态。( http://yuiblog.com/blog/2007/01 / 24 / video-crockford-tjpl /

This doesn't directly answer your question, but: 这并不直接回答您的问题,但是:

A lot of people are bothered by the between-browser inconsistencies. 许多人都被浏览器之间的不一致所困扰。 While a few folks become really good at ironing out the differences in their own JavaScript code, most can't afford to spare the time. 虽然有些人真的很擅长解决他们自己的JavaScript代码中的差异,但大多数人都不能浪费时间。 This is why there is such a profusion of frameworks available to do the dirty work for you. 这就是为什么有这么多的框架可以为你做脏工作。 JQuery is the most popular of these, I think, and I'd recommend it to you as an alternative to swallowing a lot of Aspirin for your headaches. 我认为JQuery是其中最受欢迎的,我建议你将它作为吞咽大量阿司匹林的替代方案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM