Board logo

标题: [原创代码] 第一个JS程序:Hello, World! [打印本页]

作者: zqz0012005    时间: 2011-5-21 15:14     标题: 第一个JS程序:Hello, World!

  1. function echo()
  2. {
  3.     var msg = '';
  4.     for (var i = 0; i < arguments.length; i++)
  5.     {
  6.         msg += arguments[i] + ' ';
  7.     }
  8.     msg = msg.slice(0,-1);
  9.     WScript.Echo(msg);
  10.     return msg;
  11. }
  12. function alert(){return echo.apply({},arguments);}
  13. var print = new Function("return echo.apply(this,arguments);");
  14. var msg = echo;
  15. var msgbox = echo.prototype.constructor;
  16. echo('Hello, World!');
  17. alert('Hello, World!');
  18. print(/Hello, World!/.source);
  19. msg(['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!'].join(''));
  20. msgbox({msg:'Hello, World!'}.msg);
  21. var o = {msg:'Hello, World!', say:msg};
  22. o.say(o.msg);
  23. o.say(o['msg'], 'from', 'bathome');
复制代码

作者: plp626    时间: 2011-5-21 15:18

hello,world! 也这么长。。。?
作者: zqz0012005    时间: 2011-5-21 15:24

把大家吓到了。。。
其实本来很简单,我只是想试试把目前我所知道的一些脚本基础知识综合起来会有什么效果。
作者: powerbat    时间: 2011-5-21 15:52

NB!
函数(参数个数可变)、数组、正则表达式、对象、我说不上来的,什么都用上了。。。
var msg = echo; //这个类似于C++的引用吗?
var msgbox = echo.prototype.constructor; //这个不懂,感觉好深奥
var o = {msg:'Hello, World!', say:msg}; //这个大概是JSON吧

print(/Hello, World!/.source);
这个很巧妙,不用引号也能得到字符串。这个source属性还真没注意过,没想到有这么好的用途。
apply()方法,手册写的是“应用某一对象的一个方法,用另一个对象替换当前对象”,不懂,还得再学习。
vbs中自定义函数参数个数不能变,没想到js可以,学习了。
作者: powerbat    时间: 2011-5-21 15:58

function alert(){return echo.apply({},arguments);}
var print = new Function("return echo.apply(this,arguments);");
这两种定义函数的方式有什么区别?
作者: wc726842270    时间: 2011-5-22 02:37

虽说没接触多少JS,但感上JS还是应该和VBS对比着学,这样才能知道优点缺点,在应用中也能取常补短,
作者: powerbat    时间: 2011-5-22 11:19

从楼主的代码可以看到js至少有如下优点:
1、正则表达式超级方便
2、支持参数个数可变的函数
3、两种引号都可以使用,也很方便。不像vbs要得到一个引号还得写成两个或者用chr(34)函数得到。当然js也有更通用的转义符(\)
4、创建对象不需要专门加个set,免得搞不清什么时候要加什么时候不能加。
5、有类似于“引用”的概念。比如var msg = echo 如果是在vbs中,则是把echo函数的返回值赋给msg
6、JSON据说是个好东西
……
作者: Demon    时间: 2011-6-10 12:41

function alert(){return echo.apply({},arguments);}
var print = new Function("return echo.apply(this,arguments);");
这两种定义函数的方式有什么区别?
powerbat 发表于 2011-5-21 15:58

没有实质区别,但是不建议用new Function,因为效率会稍微低一点。

详见《Professional JavaScript for Web Developers 2nd Edition》,Chapter 5
The last way to define functions is by using the  Function  constructor, which accepts any number of arguments. The last argument is always considered to be the function body, and the previous arguments enumerate the new function's arguments. Take this for example:

var sum = new Function("num1", "num2", "return num1 + num2");   //not recommended

Technically this is a function expression. This syntax is not recommended because it causes a double interpretation of the code (once for the regular ECMAScript code and once for the strings that are passed into the constructor), and thus can affect performance. However, it's important to think of functions as objects, and function names as pointers  —  this syntax is great at representing that concept.

作者: 752129471    时间: 2011-6-26 11:28

8# Demon


用c语言比这简单多了。
作者: taqiao    时间: 2011-11-24 21:35

太 长 了 吧
作者: hwliu11    时间: 2011-12-10 00:00

JS比VBS更像C/C++语言,有这类基础学起来一定很快
作者: pinke8    时间: 2012-7-11 10:39

貌似有些复杂,但这也是一个经典的程序啊,
作者: 乱码    时间: 2012-7-16 15:06

WSH下的JS,比较有意思、、




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2