123
返回列表 发新帖
楼主: justforregister

Java development 2.0: JavaScript for Java developers

[复制链接]
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
21#
 楼主| 发表于 2011-5-31 00:02 | 只看该作者
Listing 8. Ruby doesn't play that way

> array = ["A", "B"]
> ans = 2 * array


The Ruby code in Listing 8 would error out with:

TypeError: Array
can't be coerced into Fixnum

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
22#
 楼主| 发表于 2011-5-31 00:02 | 只看该作者
And if I tried to add "A" to array, it would yield:

TypeError: can't convert
Array into String


If I tried the same trick in Groovy, I would get something like this:

groovy.lang.MissingMethodException: No signature of method:
java.lang.Integer.plus() is applicable for argument types: (java.util.ArrayList) values:
[[A, B]]


Thus, you can see the various levels of weak typing in action. Clearly, if there was a scale for type weakness, JavaScript would be the wimpiest of them all!

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
23#
 楼主| 发表于 2011-5-31 00:02 | 只看该作者
JavaScript functions

A JavaScript function, like a Java method, is a construct for defining and encapsulating reusable behavior. Functions in JavaScript look and feel at first glance a lot like Groovy's closures. Functions in JavaScript are objects. In fact, they are first-class objects, very much unlike methods in Java code. Because JavaScript functions are objects, they can be passed around to other functions and can be invoked at will.

Functions are defined with the function keyword. You can specify arguments like you would in a method declaration in the Java language, plus you can return something from a JavaScript function. Unlike dynamic languages, like Groovy or Ruby, where a return call is optional (and thus the last line of any method is returned), functions in JavaScript must use a return statement if they want to return something; otherwise, they return nothing.

You call functions in JavaScript like you would call closures in Groovy. In Listing 9, I define a simple function that takes no parameters. Its purpose is to print "blah" to the JavaScript console in Chrome.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
24#
 楼主| 发表于 2011-5-31 00:02 | 只看该作者
Listing 9. Defining and calling a function in JavaScript

> function blah() { console.log("blah"); }
> blah() //prints blah
> blah.call() //prints blah
> blah.apply() //prints blah


You can invoke a function directly with parentheses (that is, ()), via the call method, or via the apply method. The first class-ness of function objects is clearly demonstrated here. Listing 10 shows what happens when I try calling a garbage method on function blah:

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
25#
 楼主| 发表于 2011-5-31 00:02 | 只看该作者
Listing 10. Functions as objects in JavaScript

> blah.foo()


In this case, you should see an error message complaining that foo isn't a method defined, like so:

TypeError: Object function blah() { console.log("blah"); } has no method 'foo'


Now read that error message again. It's complaining that foo isn't defined, which implies that if it was defined, things should have worked.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
26#
 楼主| 发表于 2011-5-31 00:02 | 只看该作者
Classes in JavaScript

JavaScript supports primitives, which I've discussed. It also supports objects, like Array. JavaScript doesn't support classes — at least not in the classical Java language sense. Because JavaScript is a prototype-based language, you don't define classes: instead, behavior is reused via cloning existing objects. Thus, in JavaScript, you don't define class objects, you define them in functions, then use nested functions to define behavior — something you've already seen in action.

To emulate a class, you define a function. You can give it a name (that is, a class name), specify parameters (as in a constructor), and even use the .this keyword, which basically means referencing a variable within the scope of the function. What's more, inner functions can be aliased to look like method calls.

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
27#
 楼主| 发表于 2011-5-31 00:03 | 只看该作者
To demonstrate, in Listing 11 I'll create a Message prototype (also known as a class) that is super simple. I'll provide a few parameters (who the message is from, who it is to, and the actual message) and the class will represent my message as JSON.

Listing 11. Functions as classes in JavaScript

function Message(to, from, msg){
this.to = to;
this.from = from;
this.msg = msg;

this.asJSON = function(){
  return "{'to':'" + this.to + "', 'from':'" + this.from + "', 'message':'" +
    this.msg + "'}";
}
}

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
28#
 楼主| 发表于 2011-5-31 00:03 | 只看该作者
In Listing 11, I defined a Message function — an object with a name and a few properties; namely, to, from, and msg. I then defined a property (asJSON) pointing to an inner function whose job it is to hand-jam a string representation of my JSON message.

Note that I can also define this "class" in a web page, load it with Chrome, open up the JavaScript console, and use it interactively. That's what's happening in Listing 12:

Listing 12. Using classes in JavaScript

> var message = new Message('Andy', 'Joe', 'Party tonight!');
> message.asJSON();
"{'to':'Andy', 'from':'Joe', 'message':'Party tonight!'}"

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
29#
 楼主| 发表于 2011-5-31 00:03 | 只看该作者
This code looks almost like Groovy or even Java code (if not for the var), doesn't it? The fact is, it's totally possible to use OOP techniques for building JavaScript applications (that is, programs containing objects with data and methods interacting with each other).

使用道具 举报

回复
论坛徽章:
131
乌索普
日期:2017-09-26 13:06:30马上加薪
日期:2014-11-22 01:34:242014年世界杯参赛球队: 尼日利亚
日期:2014-06-17 15:23:23马上有对象
日期:2014-05-11 19:35:172014年新春福章
日期:2014-04-04 16:16:58马上有对象
日期:2014-03-08 16:50:54马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14
30#
 楼主| 发表于 2011-5-31 00:03 | 只看该作者
In conclusion

I hope this article has discredited the notion, now long outdated, of JavaScript as a toy language. In fact it's quite powerful and has a lot of the syntactic sugar and ease of newer languages like Groovy and Ruby. Some of the very qualities that made JavaScript suspect in the '90s are considered desirable today.

Given the importance of the web to much Java application development and the unique place JavaScript holds as a browser-compatible language, every Java programmer should be conversant in JavaScript — period. The browser (whether it be on a computer or mobile device, phone, or tablet) is the means by which more and more people interact with applications. JavaScript is also the common medium between all server-side languages. Besides, understanding some of JavaScript's features will help make you a better programmer in any language, including the one you call home.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表