function 测试(){var a = 100;function abc(){alert(a);}return abc()}var abc=测试();abc();
var obj={};function 测试(){var a= 100;function abc(){alert(a);}obj.abc=abc;}测试();aler(obj.abc)
把abc()赋值到 obj.的方法上去
window 对象 和 navigator 对象
function 测试(){var a = 100;function abc(){alert(a);}windos.test =abc;}测试();test();
三种改装方法
1.定义window 为{};
var window = {}; var w=""!function(w) {function i() { var a = 5; return a;}w.Hex = i;} (window);//运行就需要封装成一个函数function test(){ return window.Hex()}
2.把要运行的函数Hex()定义成程序集变量.
var window = {}; var Hex ={};!function(w) {function i() { var a = 5; return a;} Hex = i;} (window);//把要运行的函数Hex()定义成程序集变量function test(){ return Hex()}
3.
var window = {}; !function(w) {function i() { var a = 5; return a;} Hex = i;} (window);//运行完自运行函数之后,定义HEX =WINDOW.HEX Hex= window.Hex; function test(){ return Hex()}
4.把函数和值都赋值到一个函数中
function add(a,b){return a+b;}function test(a,b,f){return f(a,b);}alert( test(5,6,add));
this 是本函数的意思
function test(){var a= 100;alert(this.a);}
function test(){ alert(this); } var obj={ c:function(){ alert(this);}} obj.c();
function People(){this.姓名="张三";}var people = new peopl("张三");var people1 = new peopl("李四");alert(people.姓名)
call apply
var a =300;function test(b){c=this.a+b;alert(this.c);}var obj = {a:100;}var obj1 = {a:200;}test.call(obj,200,,,,); //第一个参数放对象,第二个参数放参数.test.apply(obj,[200]); //第一个参数放对象,第二个参数放数组.