- // // 自调用函数
- // (function (形参) {
- // var num=10;// 局部变量
- // })(实参);
- // console.log(num);
- (function (win) {
- var num=10;
- //JS 是一门动态语言, 对象没有属性, 点了就有了
- win.num=num;// 把局部变量给 Windows
- })(Windows);
- console.log(num);
- (function (Windows) {
- // 产生随机数的构造函数
- function Rendom() {
- }
- // 在原型对象中添加方法
- Rendom.prototype.getRandom=function (min,max) {
- return Math.floor(Math.random()*(max-min)+min);
- };
- // 把 Random 对象暴露给顶级对象 Windows----> 外部可以直接使用这个对象
- Windows.Random=Rendom;
- })(Windows);
- var rm=new Random();
- console.log(rm.getRandom(0,5));
来源: http://www.bubuko.com/infodetail-2872765.html