找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 35120|回复: 12

[原创代码] 第一个JS程序:Hello, World!

[复制链接]
发表于 2011-5-21 15:14:30 | 显示全部楼层 |阅读模式

  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');
复制代码
发表于 2011-5-21 15:18:35 | 显示全部楼层
hello,world! 也这么长。。。?
 楼主| 发表于 2011-5-21 15:24:32 | 显示全部楼层
把大家吓到了。。。
其实本来很简单,我只是想试试把目前我所知道的一些脚本基础知识综合起来会有什么效果。
发表于 2011-5-21 15:52:45 | 显示全部楼层
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可以,学习了。
发表于 2011-5-21 15:58:21 | 显示全部楼层
function alert(){return echo.apply({},arguments);}
var print = new Function("return echo.apply(this,arguments);");
这两种定义函数的方式有什么区别?
发表于 2011-5-22 02:37:07 | 显示全部楼层
虽说没接触多少JS,但感上JS还是应该和VBS对比着学,这样才能知道优点缺点,在应用中也能取常补短,
发表于 2011-5-22 11:19:49 | 显示全部楼层
从楼主的代码可以看到js至少有如下优点:
1、正则表达式超级方便
2、支持参数个数可变的函数
3、两种引号都可以使用,也很方便。不像vbs要得到一个引号还得写成两个或者用chr(34)函数得到。当然js也有更通用的转义符(\)
4、创建对象不需要专门加个set,免得搞不清什么时候要加什么时候不能加。
5、有类似于“引用”的概念。比如var msg = echo 如果是在vbs中,则是把echo函数的返回值赋给msg
6、JSON据说是个好东西
……
发表于 2011-6-10 12:41:14 | 显示全部楼层
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.
发表于 2011-6-26 11:28:30 | 显示全部楼层
8# Demon


用c语言比这简单多了。
发表于 2011-11-24 21:35:08 | 显示全部楼层
太 长 了 吧
发表于 2011-12-10 00:00:22 | 显示全部楼层
JS比VBS更像C/C++语言,有这类基础学起来一定很快
发表于 2012-7-11 10:39:06 | 显示全部楼层
貌似有些复杂,但这也是一个经典的程序啊,
发表于 2012-7-16 15:06:24 | 显示全部楼层
WSH下的JS,比较有意思、、
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 05:28 , Processed in 0.022800 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表