FunctionÓëfunction
ECMAScript ×îÁîÈ˸ÐÐËȤµÄ¿ÉÄÜĪ¹ýÓÚº¯Êýʵ¼ÊÉÏÊǹ¦ÄÜÍêÕûµÄ¶ÔÏó¡£Function Àà¿ÉÒÔ±íʾ¿ª·¢Õ߶¨ÒåµÄÈκκ¯Êý¡£
Óà Function ÀàÖ±½Ó´´½¨º¯ÊýµÄÓï·¨ÈçÏ£º
var function_name = new function(arg1, arg2, ..., argN, function_body)
ÔÚÉÏÃæµÄÐÎʽÖУ¬Ã¿¸ö arg ¶¼ÊÇÒ»¸ö²ÎÊý£¨×î¶à 25 ¸ö£©£¬×îºóÒ»¸ö²ÎÊýÊǺ¯ÊýÖ÷Ì壨ҪִÐеĴúÂ룩¡£ÕâЩ²ÎÊý±ØÐëÊÇ×Ö·û´®¡£
¼ÇµÃÏÂÃæÕâ¸öº¯ÊýÂð£¿
- function sayHi(sName, sMessage) {
- alert("Hello " + sName + sMessage);
- }
»¹¿ÉÒÔÕâÑù¶¨ÒåËü£º
- var sayHi = new Function("sName", "sMessage", "alert(\"Hello \" + sName + sMessage);");
ËäÈ»ÓÉÓÚ×Ö·û´®µÄ¹Øϵ£¬ÕâÖÖÐÎʽдÆðÀ´ÓÐЩÀ§ÄÑ£¬µ«ÓÐÖúÓÚÀí½âº¯ÊýÖ»²»¹ýÊÇÒ»ÖÖÒýÓÃÀàÐÍ£¬ËüÃǵÄÐÐΪÓëÓà Function ÀàÃ÷È·´´½¨µÄº¯ÊýÐÐΪÊÇÏàͬµÄ¡£
Çë¿´ÏÂÃæÕâ¸öÀý×Ó£º
- function doAdd(iNum) {
- alert(iNum + 20);
- }
- function doAdd(iNum) {
- alert(iNum + 10);
- }
doAdd(10); //Êä³ö "20"
ÈçÄãËùÖª£¬µÚ¶þ¸öº¯Êý¸²¸ÇÁ˵ÚÒ»¸öº¯Êý£¬Ê¹ doAdd(10) Êä³öÁË “20”£¬¶ø²»ÊÇ “30”¡£
Èç¹ûÒÔÏÂÃæµÄÐÎʽÖØд¸Ã´úÂë¿é£¬Õâ¸ö¸ÅÄî¾ÍÇå³þÁË£º
- var doAdd = new Function("iNum", "alert(iNum + 20)");
- var doAdd = new Function("iNum", "alert(iNum + 10)");
- doAdd(10);
Çë¹Û²ìÕâ¶Î´úÂ룬ºÜÏÔÈ»£¬doAdd µÄÖµ±»¸Ä³ÉÁËÖ¸Ïò²»Í¬¶ÔÏóµÄÖ¸Õë¡£º¯ÊýÃûÖ»ÊÇÖ¸Ïòº¯Êý¶ÔÏóµÄÒýÓÃÖµ£¬ÐÐΪ¾ÍÏñÆäËû¶ÔÏóÒ»Ñù¡£ÉõÖÁ¿ÉÒÔʹÁ½¸ö±äÁ¿Ö¸Ïòͬһ¸öº¯Êý£º
- var doAdd = new Function("iNum", "alert(iNum + 10)");
- var alsodoAdd = doAdd;
- doAdd(10); //Êä³ö "20"
- alsodoAdd(10); //Êä³ö "20"
ÔÚÕâÀ±äÁ¿ doAdd ±»¶¨ÒåΪº¯Êý£¬È»ºó alsodoAdd ±»ÉùÃ÷ΪָÏòͬһ¸öº¯ÊýµÄÖ¸Õë¡£ÓÃÕâÁ½¸ö±äÁ¿¶¼¿ÉÒÔÖ´Ðиú¯ÊýµÄ´úÂ룬²¢Êä³öÏàͬµÄ½á¹û - “20”¡£Òò´Ë£¬Èç¹ûº¯ÊýÃûÖ»ÊÇ
Ö¸Ïòº¯ÊýµÄ±äÁ¿£¬ÄÇô¿ÉÒ԰Ѻ¯Êý×÷Ϊ²ÎÊý´«µÝ¸øÁíÒ»¸öº¯ÊýÂ𣿻شðÊǿ϶¨µÄ£¡
- function callAnotherFunc(fnFunction, vArgument) {
- fnFunction(vArgument);
- }
- var doAdd = new Function("iNum", "alert(iNum + 10)");
- callAnotherFunc(doAdd, 10); //Êä³ö "20"
ÔÚÉÏÃæµÄÀý×ÓÖУ¬callAnotherFunc() ÓÐÁ½¸ö²ÎÊý - Òªµ÷Óõĺ¯ÊýºÍ´«µÝ¸ø¸Ãº¯ÊýµÄ²ÎÊý¡£Õâ¶Î´úÂë°Ñ doAdd() ´«µÝ¸ø callAnotherFunc() º¯Êý£¬²ÎÊýÊÇ 10£¬Êä³ö “20”¡£
×¢Ò⣺¾¡¹Ü¿ÉÒÔʹÓà Function ¹¹Ô캯Êý´´½¨º¯Êý£¬µ«×îºÃ²»ÒªÊ¹ÓÃËü£¬ÒòΪÓÃËü¶¨Ò庯Êý±ÈÓô«Í³·½Ê½ÒªÂýµÃ¶à¡£²»¹ý£¬ËùÓк¯Êý¶¼Ó¦¿´×÷ Function ÀàµÄʵÀý¡£
ÈçÇ°ËùÊö£¬º¯ÊýÊôÓÚÒýÓÃÀàÐÍ£¬ËùÒÔËüÃÇÒ²ÓÐÊôÐԺͷ½·¨¡£
ECMAScript ¶¨ÒåµÄÊôÐÔ length ÉùÃ÷Á˺¯ÊýÆÚÍûµÄ²ÎÊý¸öÊý¡£ÀýÈ磺
- function doAdd(iNum) {
- alert(iNum + 10);
- }
- function sayHi() {
- alert("Hi");
- }
- alert(doAdd.length); //Êä³ö "1"
- alert(sayHi.length); //Êä³ö "0"
º¯Êý doAdd() ¶¨ÒåÁËÒ»¸ö²ÎÊý£¬Òò´ËËüµÄ length ÊÇ 1£»sayHi() ûÓж¨Òå²ÎÊý£¬ËùÒÔ length ÊÇ 0¡£
¼Çס£¬ÎÞÂÛ¶¨ÒåÁ˼¸¸ö²ÎÊý£¬ECMAScript ¿ÉÒÔ½ÓÊÜÈÎÒâ¶à¸ö²ÎÊý£¨×î¶à 25 ¸ö£©£¬ÕâÒ»µãÔÚÉÏÃæ½²½â¹ý¡£ÊôÐÔ length Ö»ÊÇΪ²é¿´Ä¬ÈÏÇé¿öÏÂÔ¤ÆڵIJÎÊý¸öÊýÌṩÁËÒ»ÖÖ¼ò±ã·½Ê½¡£
Function ¶ÔÏóÒ²ÓÐÓëËùÓжÔÏó¹²ÏíµÄ valueOf() ·½·¨ºÍ toString() ·½·¨¡£ÕâÁ½¸ö·½·¨·µ»ØµÄ¶¼ÊǺ¯ÊýµÄÔ´´úÂ룬ÔÚµ÷ÊÔʱÓÈÆäÓÐÓá£ÀýÈ磺
- function doAdd(iNum) {
- alert(iNum + 10);
- }
- document.write(doAdd.toString());
ÉÏÃæÕâ¶Î´úÂëÊä³öÁË doAdd() º¯ÊýµÄÎı¾¡£
functionʹÓÃ
- function myfunc(param) {
- //code
- }
×¢ÒâJavascriptÖеĺ¯ÊýÃûÏàͬµÄÁ½¸öfunction±»ÈÏΪÊÇͬһ¸ö£¬ÔÚÔËÐÐʱµ½µ×µ÷ÓÃÄÄÒ»¸öfunctionÈ¡¾öÓÚ¼ÓÔØ˳Ðò,ºóÒ»¸ö¼ÓÔصÄfunction»á¸²¸ÇÇ°Ò»¸ö.
- function func1() {return 'func1'; }
- function func1(name) { return name; }
¾¿ÆäÔÒòÊÇ£ºjavascriptÖÐfunctionµÄ²ÎÊý¶¼ÊÇ¿ÉÑ¡²ÎÊý,Òò´ËfuncitonµÄʶ±ðÊDz»°üÀ¨Èë²ÎµÄ,¶øº¯ÊýÈë²Î´¦µÄÉùÃ÷ÊÇΪÁËÒýÓ÷½±ãÒÔ¼°¿É¶ÁÐÔ.
ÒÔÉϵĴúÂëÒ²µÈ¼ÛÓÚ:
- function func1() {
- return arguments[0] || 'func1';
- }
- func(); //return 'func1'
- func('function'); //return 'function'
functionµ±×÷¶ÔÏó
ÊǵÄû´í,ÔÚjavascriptÖÐfunction¾ÍÊǶÔÏó,ÎÒÃÇ¿ÉÒÔÏòʹÓÃÒ»¸ö¶ÔÏóÄÇÑùʹÓÃfunction¡£Ëü¿ÉÒÔÓÐ×Ô¼ºµÄÊôÐԺͷ½·¨.ÓÐÈçϵÄÒ»¸öfunciton:
- function nameOf(name) {
- return name.toUpperCase();
- }
- function×÷Ϊ¶ÔÏó½øÐи³Öµ
- var person = person || {};
- person.nameOf = nameOf;
- person.nameOf('yang dong') // return "YANG DONG"
¶¨ÒåfunctionµÄÊôÐÔ
¿´¿´ÒÔϵĴúÂë, ÄãÄÜ´ÓÖеĵ½Ê²Ã´ÐÅÏ¢:
- function nameOf() {return nameOf.blogger;}
- nameOf.blogger = "YANG_DONG";
û´í,function¿ÉÒÔÓµÓÐ×Ô¼ºµÄÊôÐÔ¡£¿¼ÂÇÕâÑùÒ»ÖÖ³¡¾°, ¼ÙÈçÎÒÃÇҪͳ¼Æij¸öfunction±»µ÷ÓõĴÎÊý.ÄÇôÎÒÃÇÓÐÁ½ÖÖ·½Ê½À´ÊµÏÖ:
1.É趨һ¸öÈ«¾Ö±äÁ¿À´¼Ç¼,¸Ãfunciton±»µ÷ÓõĴÎÊý,ÿµ÷ÓÃÒ»´Î,¸Ã±äÁ¿¼Ó1:
- var globalNameOfCounter = 0;
- nameOf();
- globalNameOfCounter ++;
ÕâÑù×ö¿´ÆðÀ´ÊÇûÓÐÎÊÌâµÄ,ÔÚ´úÂ뻹ÊDZȽϼòµ¥µÄʱºò,Ëü¿ÉÒÔ¹¤×÷µÄºÜºÃ,µ«ÊÇËæ×Å´úÂëÔ½À´Ô½¸´ÔÓ,ά»¤Õâ¶ÎÂß¼µÄ³É±¾»áÖ±ÏßÉÏÉý¡£Ö÷ÒªÊÇÒòΪ:globalNameOfCounterÎÛȾµÄglobalÃüÃû¿Õ¼ä,²¢ÇÒÆÆ»µÁË´úÂëµÄ·â×°ÐÔ.
2.ʹÓÃfunctionµÄÊôÐÔ
¿´¿´ÒÔÏ´úÂë:
- function nameOf() {
- nameOf.counter++;
- return nameOf.blogger;
- }
- nameOf.blogger = “YANG_DONG"
- nameOf.counter = 0;
- nameOf(); //nameOf.counter = 1
- nameOf(); //nameOf.counter = 2
ÏÔ¶øÒ×¼û,µÚ¶þÖÖ·½Ê½ÓÐןܺõķâ×°ÐÔºÍά»¤ÐÔ.functionµÄÊôÐÔµÄÓ¦Óû¹²»Ö¹Èç´Ë.Çë¿´ÏÂÎÄ.
function×÷ΪÃû×Ö¿Õ¼ä
Javascript²»Ö§³ÖÃû×Ö¿Õ¼ä(±¾È˲»Ì«Àí½âÈç´ËÇ¿´óµÄÓïÑÔΪʲô²»Ö§³ÖÃû×Ö¿Õ¼äÄØ,ÕâÊÇΪʲôÄØ?),
²»¹ýÎÒÃÇÒÀÈ»¿ÉÒÔʹÓÃÆäÇ¿´óµÄfuncitonΪÎÒÃÇÖ§³ÖÃû×Ö¿Õ¼ä.
´ÓÉϽÚÎÒÃÇÖªµÀfunction¿ÉÒÔ¶¨Òå×Ô¼ºµÄÊôÐÔ,ÎÒÃǾͿÉÒÔÀûÓøÃÌØÐÔÀ´Éú³ÉÃû×Ö¿Õ¼ä.Çë¿´ÒÔÏ´úÂë:
- nameOf.getBloggerName = function() {
- return nameOf.blogger;
- }
´ËʱÔÚnameOfÃû×Ö¿Õ¼äÖ®ÏÂÒѾ°üº¬ÁË:blogger,counterÊôÐÔºÍfunction getBloggerName.
function×÷Ϊmethod
ÔÚjavascriptÖÐfunctionºÍmethodÆäʵÊÇûÓÐʲô±¾ÖÊÇø±ðµÄ,Èç¹û·ÇµÄÇø·ÖÁ½ÕߵĻ°,ÎÒÏëÒ²¾ÍÊÇthis±äÁ¿²»Í¬°É.
- function g() {return this;}
- var local = local || {};
- local.method = g; //ÐÞ¸ÄthisÖ¸Ïòlocal
- local.method(); //·µ»Ølocal¶ÔÏó
- g(); //·µ»ØDOMWindow¶ÔÏó
function½ÔΪclosure
ÔÚJavascriptÖÐËùÓеÄfunction¶¼°ó¶¨ÁËÒ»¸öscope chain,Òò´ËËüÊÇÒ»¸ö±£´æÁ˵÷ÓÃÉÏÏÂÎĵĺ¯Êý.¿´¿´ÏÂÃæµÄʵÀý´úÂë:
- var variable = 'global';
- function getVariable(){
- var variable = 'local',
- func = function() {
- return variable;
- };
- return func;
- }
- getVariable()(); //return local;
µ±func±»µ÷ÓõÄʱºò,ËüËùÈ¡µÄvaribleµÄÖµÊǵ÷ÓÃÉÏÏÂÎÄÖеıäÁ¿¶ø²¢·ÇÓë֮ͬÃûµÄÈ«¾Ö±äÁ¿.
×ܽá
Èç¹ûÓÃÒ»¾ä»°¸ÅÀ¨½ñÌì¶ÔfuncitonµÄ½éÉÜ,ÄÇôÎÒÏëÓ¦¸ÃÊÇ: functionÊÇ¿ÉÒÔ±»µ÷ÓÃÖ´ÐеĴúÂ뼯¶ÔÏó.
ÒÔÉÏÊÇfunctionµÄһЩӦÓó¡¾°,µ±È»Ëü»¹²»Ö¹ÕâЩ.
±ÈÈç: function×÷Ϊ¹¹Ô캯Êý(Ò²¾ÍÊÇfunciton×÷ΪÀà),¼Ì³ÐµÈ.ÕâЩÄÚÈݽ«ÔÚÒÔºó´ó¼Ò½éÉÜ,¾´ÇëÆÚ´ý.
Function¶ÔÏóµÄÊôÐԺͷ½·¨
º¯ÊýÊÇ JavaScript ÖеÄÒýÓÃÊý¾ÝÀàÐÍ£¬ÔÚº¯ÊýÕâ¸ö¶ÔÏóÉ϶¨ÒåÁËһЩÊôÐԺͷ½·¨£¬ÏÂÃæÎÒÃÇÖðÒ»À´½éÉÜÕâЩÊôÐԺͷ½·¨£¬Õâ¶ÔÓÚÀí½âJavascriptµÄ¼Ì³Ð»úÖƾßÓÐÒ»¶¨µÄ°ïÖú¡£
ÊôÐÔ(Properties)
arguments
»ñÈ¡µ±Ç°ÕýÔÚÖ´ÐÐµÄ Function ¶ÔÏóµÄËùÓвÎÊý£¬ÊÇÒ»¸öÀàËÆÊý×鵫²»ÊÇÊý×éµÄ¶ÔÏó£¬ËµËüÀàËÆÊý×éÊÇÒòΪÆä¾ßÓÐÊý×éÒ»ÑùµÄ·ÃÎÊÐÔÖʼ°·½Ê½£¬¿ÉÒÔÓÉarguments[n]À´·ÃÎʶÔÓ¦µÄµ¥¸ö²ÎÊýµÄÖµ£¬²¢ÓµÓÐÊý×鳤¶ÈÊôÐÔlength¡£»¹ÓоÍÊÇarguments¶ÔÏó´æ´¢µÄÊÇʵ¼Ê´«µÝ¸øº¯ÊýµÄ²ÎÊý£¬¶ø²»¾ÖÏÞÓÚº¯ÊýÉùÃ÷Ëù¶¨ÒåµÄ²ÎÊýÁÐ±í£¨length)£¬¶øÇÒ²»ÄÜÏÔʽ´´½¨ arguments ¶ÔÏó¡£ÏÂÃæµÄSample˵Ã÷ÁËÕâЩÐÔÖÊ¡£
- function testArg(a, b) {
- var actCount = arguments.length,
- expCount = testArg.length,
- result;
- result = "Expected arguments' count is " + expCount + ";<br/>";
- result += "Actual arguments' count is " + actCount + ".<br/>";
- result += "They are:<br/>";
- for (var i = 0; i < actCount; i++) {
- result += arguments[i] + ";<br/>";
- }
- if (arguments instanceof Array) {
- result += "arguments is an Array instance."
- } else if (arguments instanceof Object) {
- result += "arguments is an Object instance."
- }
- document.write(result);
- }
- testArg(1);
- //output result is:
Expected arguments' count is 2;
Actual arguments' count is 1.
They are:
1;
arguments is an Object instance.
####length
»ñÈ¡º¯Êý¶¨ÒåµÄ²ÎÊý¸öÊý£¬functionName.length
²»Í¬ÓÚarguments.length£¬ÕâµãÎÒÃÇÔÚÉÏÃæÓнéÉÜ¡£ÒòΪJavascriptµ÷Óú¯Êýʱºò¶Ôº¯Êý²ÎÊý²»×÷ÈκθöÊýºÍÀàÐͼì²é£¬Ò²¾ÍûÓк¯Êýµ÷ÓôíÎó¸ÅÄî¡£µ«ÊÇÎÒÃÇ¿ÉÒÔÀûÓÃfunctionName.lengthºÍarguments.lengthµÄ²»Í¬£¬ÔÚº¯Êýµ÷ÓÃÄÚ²¿À´¼ì²â²ÎÊý¸öÊý¼ì²â¡£
- function checkVarCount(a, b) {
- if (checkVarCount.length !== arguments.length) {
- alert("The count of the parameters you passed into the function doesn't match the function definition.");
- }
- alert("Successfully call the function");
- }
- checkVarCount(1, 2);
- //Successfully call the function
- checkVarCount(1);
- //The count of the parameters you passed into the function doesn't match the function definition.
caller»ñÈ¡µ÷Óõ±Ç°º¯ÊýµÄº¯Êý¡£callerÊôÐÔÖ»Óе±º¯ÊýÕýÔÚÖ´ÐÐʱ²Å±»¶¨Òå¡£
functionName.caller
Èç¹ûº¯ÊýÊÇ´Ó JavaScript ³ÌÐòµÄ¶¥²ãµ÷Óõģ¬Ôòcaller°üº¬null¡£Èç¹ûÔÚ×Ö·û´®ÉÏÏÂÎÄÖÐʹÓà caller ÊôÐÔ£¬ÔòÆä½á¹ûºÍ functionName.toString Ïàͬ£¬Ò²¾ÍÊÇ˵£¬½«ÏÔʾº¯ÊýµÄ·´±àÒëÎı¾¡£
- function test() {
- if (test.caller == null) {
- document.write("test is called from the toppest level");
- } else {
- document.write("test is called from the function:<br/>");
- document.writeln(test.caller.toString());
- }
- document.write("<br />");
- }
- //call from the top level
- test();
- //output: test is called from the toppest level
- function testOuter() {
- test();
- }
- //call from the function testOuter
- testOuter();
- //output:
- //test is called from the function:
- //function testOuter() { test(); }
- ####callee
·µ»ØÕý±»Ö´ÐÐµÄ Function ¶ÔÏ󣬼´Ö¸¶¨µÄ Function ¶ÔÏóµÄÕýÎÄ¡£
[functionName.]arguments.callee
callee ÊôÐÔÊÇ arguments ¶ÔÏóµÄÒ»¸ö³ÉÔ±£¬¸ÃÊôÐÔ½öµ±Ïà¹Øº¯ÊýÕýÔÚÖ´ÐÐʱ²Å¿ÉÓá£Í¨³£Õâ¸öÊôÐÔ±»ÓÃÀ´µÝ¹éµ÷ÓÃÄäÃûº¯Êý¡£
- var fac = function(n){
- if (n <= 0)
- return 1;
- else
- return n * arguments.callee(n - 1);
- }(4);
- document.write(fac);//24
constructor
»ñÈ¡´´½¨Ä³¸ö¶ÔÏóµÄº¯Êý¡£constructor ÊôÐÔÊÇÿ¸ö¾ßÓÐÔÐ͵ĶÔÏóµÄÔÐͳÉÔ±¡£ Õâ°üÀ¨³ý Global ºÍ Math ¶ÔÏóÖ®ÍâµÄËùÓÐÄÚ²¿ JavaScript ¶ÔÏó¡£ constructor ÊôÐÔ¾ÍÊÇÓÃÀ´¹¹Ôì¶ÔÏóʵÀýµÄº¯ÊýÒýÓá£
- // A constructor function.
- function MyObj() {
- this.number = 1;
- }
- var x = new String("Hi");
- if (x.constructor == String)
- document.write("Object is a String.");
- document.write ("<br />");
- var y = new MyObj;
- if (y.constructor == MyObj)
- document.write("Object constructor is MyObj.");
- // Output:
- // Object is a String.
- // Object constructor is MyObj.
prototype»ñÈ¡¶ÔÏóµÄÔÐÍ¡£Ã¿Ò»¸ö¹¹Ô캯Êý¶¼ÓÐÒ»¸öprototypeÊôÐÔ£¬Ö¸ÏòÁíÒ»¸ö¶ÔÏó¡£Õâ¸ö¶ÔÏóµÄËùÓÐÊôÐԺͷ½·¨£¬¶¼»á±»¹¹Ô캯ÊýµÄʵÀý¼Ì³Ð¡£ÕâÒâζ×Å£¬ÎÒÃÇ¿ÉÒÔ°ÑÄÇЩ²»±äµÄÊôÐԺͷ½·¨£¬Ö±½Ó¶¨ÒåÔÚprototype¶ÔÏóÉÏ¡£
- function Man(name, age) {
- this.name = name;
- this.age = age;
- }
- Man.prototype.sex = "M";
- Man.prototype.struggle = function () {
- alert("day day up!!!!");
- }
- var li = new Man("Leo", 10);
- alert(li.sex);//M
- li.struggle();//day day up
- Man.prototype.isStrong = true;
- alert(li.isStrong);//true
- ÕâÑùÎÒÃÇÒ²¿ÉÒÔÏòÒѶ¨ÒåºÃµÄ¶ÔÏ󣨰üÀ¨javascriptÌṩµÄÔÉú¶ÔÏó£©ÖÐ×·¼Ó·½·¨ºÍÊôÐÔ£¬
- var aa = new Number(2);
- alert(typeof (aa.add)); //undefined
- Number.prototype.add = function (add1) {
- return this + add1;
- }
- alert(aa.add(1)); // 3
·½·¨ applyµ÷Óú¯Êý£¬²¢ÓÃÖ¸¶¨¶ÔÏóÌæ»»º¯ÊýµÄthisÖµ£¬Í¬Ê±ÓÃÖ¸¶¨Êý×éÌæ»»º¯ÊýµÄ²ÎÊý¡£
functionName.apply([thisObj[,argArray]])
Èç¹ûargArrayΪÎÞЧֵ£¬Ôò»áÅ׳ö”Object expected”´íÎó£»Èç¹ûthisObjºÍargArray¶¼Ã»ÓÐÌṩ£¬Ôò»áʹÓõ±Ç°this×÷ΪthisObj
- function callMe(arg1, arg2) {
- var s = "";
- s += "this value: " + this;
- s += "<br />";
- for (i in callMe.arguments) {
- s += "arguments: " + callMe.arguments[i];
- s += "<br />";
- }
- return s;
- }
- document.write("Original function: <br/>");
- document.write(callMe(1, 2));
- document.write("<br/>");
- document.write("Function called with apply: <br/>");
- document.write(callMe.apply(3, [4, 5]));
- document.write("<br/>");
- document.write("Function called with apply with invalid array: <br/>");
- try{
- document.write(callMe.apply(3,2));
- } catch (e) {
- document.write(e.message);
- }
- document.write("<br/><br/>");
- document.write("Function called with apply without any argument: <br/>");
- document.write(callMe.apply());
- //Output result:
- //Original function:
- //this value: [object Window]
- // arguments: 1
- // arguments: 2
- //Function called with apply:
- //this value: 3
- // arguments: 4
- // arguments: 5
- //Function called with apply with invalid array:
- //Function.prototype.apply: Arguments list has wrong type
- //Function called with apply without any argument:
- //this value: [object Window]
callµ÷ÓÃÒ»¸ö¶ÔÏóµÄ·½·¨£¬ÓÃÁíÒ»¸ö¶ÔÏóÌæ»»µ±Ç°¶ÔÏó¡£
call([thisObj[, arg1[, arg2[, [, argN]]]]])
ËüÔÊÐíÄú½«º¯ÊýµÄ this ¶ÔÏó´Ó³õʼÉÏÏÂÎıäΪÓÉ thisObj Ö¸¶¨µÄжÔÏó¡£ Èç¹ûûÓÐÌṩ thisObj ²ÎÊý£¬Ôò global ¶ÔÏó±»ÓÃ×÷ thisObj¡£Óëapply·½·¨Î¨Ò»²»Í¬µÄµØ·½ÊÇ£¬applyµÄ
µÚ¶þ¸ö²ÎÊýÀàÐͱØÐëÊÇArray£¬¶øcall·½·¨Êǽ«ËùÓеIJÎÊýÁоٳöÀ´£¬ÓöººÅ·Ö¸ô¡£
- function callMe(arg1, arg2){
- var s = "";
- s += "this value: " + this;
- s += "<br />";
- for (i in callMe.arguments) {
- s += "arguments: " + callMe.arguments[i];
- s += "<br />";
- }
- return s;
- }
- document.write("Original function: <br/>");
- document.write(callMe(1, 2));
- document.write("<br/>");
- document.write("Function called with call: <br/>");
- document.write(callMe.call(3, 4, 5));
- // Output:
- // Original function:
- // this value: [object Window]
- // arguments: 1
- // arguments: 2
- // Function called with call:
- // this value: 3
- // arguments: 4
- // arguments: 5
bind
¶ÔÓÚ¸ø¶¨º¯Êý£¬´´½¨¾ßÓÐÓëÔʼº¯ÊýÏàͬµÄÖ÷ÌåµÄ°ó¶¨º¯Êý¡£ Ôڰ󶨹¦ÄÜÖУ¬this¶ÔÏó½âÎöΪ´«ÈëµÄ¶ÔÏó¡£ ¸Ã°ó¶¨º¯Êý¾ßÓÐÖ¸¶¨µÄ³õʼ²ÎÊý¡£
function.bind(thisArg[,arg1[,arg2[,argN]]])
ÆäÖÐfunction, thisArgΪ±ØÑ¡Ïî¡£·µ»ØÒ»¸öÓë function º¯ÊýÏàͬµÄк¯Êý£¬Ö»²»¹ýº¯ÊýÖеÄthis¶ÔÏóºÍ²ÎÊý²»Í¬¡£
- // Define the original function.
- var checkNumericRange = function (value) {
- if (typeof value !== 'number')
- return false;
- else
- return value >= this.minimum && value <= this.maximum;
- }
- // The range object will become the this value in the callback function.
- var range = { minimum: 10, maximum: 20 };
- // Bind the checkNumericRange function.
- var boundCheckNumericRange = checkNumericRange.bind(range);
- // Use the new function to check whether 12 is in the numeric range.
- var result = boundCheckNumericRange (12);
- document.write(result);
- // Output: true
ÒÔÏ´úÂëÑÝʾÈçºÎʹÓà arg1[,arg2[,argN]]] ²ÎÊý¡£ ¸Ã°ó¶¨º¯Êý½« bind ·½·¨ÖÐÖ¸¶¨µÄ²ÎÊýÓÃ×÷µÚÒ»¸ö²ÎÊýºÍµÚ¶þ¸ö²ÎÊý¡£ ÔÚµ÷Óøð󶨺¯Êýʱ£¬Ö¸¶¨µÄÈκβÎÊý½«ÓÃ×÷µÚÈý¸ö¡¢µÚËĸö²ÎÊý£¨ÒÀ´ËÀàÍÆ£©¡£
- // Define the original function with four parameters.
- var displayArgs = function (val1, val2, val3, val4) {
- document.write(val1 + " " + val2 + " " + val3 + " " + val4);
- }
- var emptyObject = {};
- // Create a new function that uses the 12 and "a" parameters
- // as the first and second parameters.
- var displayArgs2 = displayArgs.bind(emptyObject, 12, "a");
- // Call the new function. The "b" and "c" parameters are used
- // as the third and fourth parameters.
- displayArgs2("b", "c");
- // Output: 12 a b c
ÔÚ¶ÔÏó¶¨ÒåÄÚ²¿Ê¹ÓÃbind·½·¨¿ÉÒÔ½«Ä³¸öʼþ°ó¶¨µ½¶ÔÏóÄÚ²¿µÄij¸ö·½·¨£¬
- <input type="button" id="start" value="Start" />
- <input type="button" id="stop" value="Stop" />
- <script type="text/javascript">
- function Car(owner) {
- this.owner = owner;
- this.start = function () {
- //start the car
- console.log(this);
- //output: Car {owner: "Mike", start: function, stop: function} check.html:14
- console.log(this.owner + "'s car is starting.");
- //output: Mike's car is starting.
- };
- this.stop = function () {
- console.log(this);
- //output: <input type="button" id="stop" value="Stop" />
- console.log(this.owner + "'s car is starting.");
- //output: undefined's car is stopping.
- };
- }
- var btnStart = document.getElementById("start"),
- btnStop = document.getElementById("stop"),
- someCar = new Car("Mike");
- if (document.attachEvent) {
- btnStart.attachEvent("onClick", someCar.start.bind(someCar));
- btnStop.attachEvent("onClick", someCar.stop);
- } else if (document.addEventListener) {
- btnStart.addEventListener("click", someCar.start.bind(someCar), false);
- btnStop.addEventListener("click", someCar.stop, false);
- }
- </script>
´ÓÉÏÃæSampleÎÒÃÇ·¢ÏÖ£¬µ±²»Ê¹ÓÃbind·½·¨µÄʱºò£¬Ê¼þÀïÃæµÄthisÖ¸ÏòµÄ´¥·¢clickʼþdomÔªËØinput£¬Ëüµ±È»Ã»ÓÐownerÊôÐÔ£»Èç¹ûÀûÓÃbindÖ¸¶¨Ê¼þÀïÃæµÄthis¶ÔÏ󣬾ÍÄÜ´ïµ½ÎÒÃÇÏëÒªµÄЧ¹û¡£
toString·µ»Ø¶ÔÏóµÄ×Ö·û´®±íʾÐÎʽ¡£
objectname.toString([radix])
objectname±ØÐ裬ָ¶¨ÐèÒª»ñÈ¡×Ö·û´®±íʾÐÎʽµÄ¶ÔÏó¡£radix¿ÉÑ¡£¬Îª½«Êý×Öֵת»»Îª×Ö·û´®Ö¸¶¨Ò»¸ö»ùÊý£¬´ËÖµ½öÓÃÓÚÊý×Ö¡£
toString ·½·¨ÊÇÒ»¸öËùÓÐÄÚÖÃµÄ JavaScript ¶ÔÏóµÄ³ÉÔ±¡£ ËüµÄÐÐΪȡ¾öÓÚ¶ÔÏóµÄÀàÐÍ£º
Object Behavior
Array ½« Array µÄÔªËØת»»Îª×Ö·û´®¡£ ½á¹û×Ö·û´®±»Á¬½ÓÆðÀ´£¬ÓöººÅ·Ö¸ô¡£
Boolean Èç¹û²¼¶ûֵΪ true£¬Ôò·µ»Ø“true”¡£ ·ñÔò·µ»Ø“false”¡£
Date ·µ»ØÈÕÆÚµÄÎı¾±íʾÐÎʽ¡£
Error ·µ»ØÒ»¸ö°üº¬Ïà¹Ø´íÎóÐÅÏ¢µÄ×Ö·û´®¡£
Function ·µ»ØÈçϸñʽµÄ×Ö·û´®£¬ÆäÖÐ functionname ÊÇÒ»¸öº¯ÊýµÄÃû³Æ£¬´Ëº¯ÊýµÄ toString ·½·¨±»µ÷Óãº
function functionname( ) { [native code] }
Number ·µ»ØÊý×ÖµÄÎÄ×Ö±íʾÐÎʽ¡£
String ·µ»Ø String ¶ÔÏóµÄÖµ¡£
Default ·µ»Ø "[object objectname]"£¬ÆäÖÐ objectname Ϊ¶ÔÏóÀàÐ͵ÄÃû³Æ¡£
valueOf
·µ»Ø¶ÔÏóµÄÔÉúÖµ¡£
object.valueOf( )
JavascriptÄÚ²¿¸÷¸ö¶ÔÏó¶¨ÒåµÄvalueOf²»Í¬£º
Object Return value
Array ·µ»ØÊý×éʵÀý¡£
Boolean ²¼¶ûÖµ¡£
Date ´Ó UTC 1970 Äê 1 Ô 1 ÈÕÎçÒ¹¿ªÊ¼µÄ´æ´¢µÄʱ¼äÖµ£¨ÒÔºÁÃëΪµ¥Î»£©¡£
Function º¯Êý±¾Éí¡£
Number Êý×ÖÖµ¡£
Object ¶ÔÏó±¾Éí¡£ ÕâÊÇĬÈÏÖµ¡£
String ×Ö·û´®Öµ¡£
Math ºÍ Error ¶ÔÏó¶¼Ã»ÓÐ valueOf ·½·¨¡£
×ܽá
Function ºÍ function£¬Àí½âÁËÂð£¿
TAG:function
±¾Õ¾»¶ÓÈκÎÐÎʽµÄתÔØ£¬µ«ÇëÎñ±Ø×¢Ã÷³ö´¦£¬×ðÖØËûÈËÀͶ¯³É¹û
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-800-1.html
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-800-1.html