http://xiayuanfeng.javaeye.com/blog/£¨ÔÎÄ£©
ʲôÊǺ¯Êý(Function)
function sum(a,b){
return a+b;
}
ÆäʵͨË×µÄ˵¾ÍÊÇÒ»¸öÓÐÃû³ÆµÄ´úÂë¶Î£¬·½±ãÖØÓá£
ҪעÒâµÄÊÇ£º
1.Javascript µÄº¯ÊýÓï·¨£¬ÒòΪJavascript±¾Éí¾ÍÊÇÇø·Ö´óСдµÄ£¬ËùÒÔfunction²»ÄÜд×÷Function»òFUNCTION.
2.sumÊǺ¯ÊýµÄÃû³Æ£¬Õâ¸ö²¢²»ÊDZØÐëµÄ£¬µÈÒÔºóÎÒÃÇ»á˵µ½¡£
3.returnÊÇ·µ»Ø£¬Èç¹û²»Ð´µÄ»°£¬º¯ÊýµÄ·µ»ØÊÇundefined.Èç¹ûÒª·µ»Ø¶à¸öÖµ£¬¿ÉÒÔ·µ»Ø¸öÊý×é»òÕß¶ÔÏó£¨ÒÔºóÔÙ˵£©
º¯ÊýµÄµ÷ÓÃ
ÏÂÃæÎÒÃǽ²º¯ÊýµÄ»ù±¾µ÷Óá£
var result = sum(1,2)
º¯ÊýµÄ²ÎÊý
²»ºÃÒâ˼µÄ˵£¬Õâ¸ö²ÅÊDZ¾ÆªÎÄÕµÄÖØµã¡£
ʵÀýÒ»£¬²ÎÊýÉÙÓÚ Êµ¼Ê¶¨Òå²ÎÊýµÄÊýÄ¿
var result = sum(1);
½á¹ûresult ΪNaN£¬Õâ¾Í˵Ã÷ÁË£¬²ÎÊýbµÄֵΪundefined,µ«²¢²»»á±¨´í£¬Õâ¾ÍÎÞÐÎÖÐÖÆÔìÁËbug.
ʵÀý¶þ£¬²ÎÊý¶àÓÚ Êµ¼Ê¶¨Òå²ÎÊýµÄÊýÄ¿
sum(1,2,3,4,5)
½á¹ûΪ3.·¢ÏÖ¶àÓڵIJÎÊý±»ºöÂÔÁË¡£
ʵÀýÈý£¬Ã»ÓвÎÊýµÄº¯Êý
function args(){return arguments;}
ÿ¸öº¯ÊýÀï¶¼ÓÐÒ»¸öĬÈϵÄÊý×éÄǾÍÊÇarguments .Ëü¾ÍÊÇÿ¸öº¯ÊýĬÈϵIJÎÊýΪ[] ¡£Èç¹ûÎÒÃǵ÷Óú¯ ......
Several programming languages implement a sprintf function, to output a formatted string. It originated from the C programming language, printf function. Its a string manipulation function.
This is limited sprintf Javascript implementation. Function returns a string formatted by the usual printf conventions. See below for more details. You must specify the string and how to format the variables in it. Possible format values:
%% – Returns a percent sign
%b – Binary number
%c – The character according to the ASCII value
%d – Signed decimal number
%f – Floating-point number
%o – Octal number
%s – String
%x – Hexadecimal number (lowercase letters)
%X – Hexadecimal number (uppercase letters)
Additional format values. These are placed between the % and the letter (example %.2f):
+ (Forces both + and – in front of numbers. By default, only negative numbers are marked)
– (Left-justifies the variable value ......
JavascriptµÄ¼Ì³Ð¿ÉÒÔͨ¹ýcall¡¢apply¡¢prototypeʵÏÖ¡£
1¡¢call£ºÔÚ×ÓÀàÖУ¬Óø¸Àà.call£¨this£¬arg0£¬arg1...£©¿ÉÒԼ̳и¸Àà¡£×¢ÒâcallµÄλÖ㬾¡Á¿ÔÚ×ÓÀàµÄµÚÒ»ÐУ¨js°´Ë³ÐòÖ´ÐУ¬·ÅÔÚºóÃæ¿ÉÄܶÔ×ÓÀàµÄÆäËûÊôÐÔ¡¢·½·¨ÓÐÓ°Ïì¡£±ÈÈç×ÓÀàºÍ¸¸ÀàÓÐÏàͬÃû×ֵķ½·¨£¬ºóÃæµÄ¸²¸ÇÇ°ÃæµÄ£©¡£
<html>
<head>
<title>call\apply\prototype test</title>
</head>
<script type="text/javascript">
//personÀà
function person(name, age) {
this.name = name;
this.age = age;
this.say = function() {
document.write("I am a person");
}
this.display = function() {
document.write(this.name + "-" + this.age);
}
}
//studentÀ࣬¼Ì³Ð×Ôperson
function student(name, age, no) {
person.call(this, name, age);//personÖеÄthisµÈÓÚ²ÎÊýÖеÄthis£¬¼´studentÖеÄnameºÍage
this.no = no;
this.display = function() {//¸²¸ÇÁ˸¸ÀàµÄ·½·¨
document.write(this.name + "-" + this.age + "-" + this.no);
}
}
//´´½¨personÀà
var p = new person("captain", 21);
p.display ......
ÕâÀÂǵÄÊÇ.net·þÎñÆ÷¿Ø¼þcheckbox»òcheckboxList£»
¼ÙÉèÒ³ÃæÈçÏ£¬chkDepartÊDz¿ÃÅ£¬chkPeopleÊÇËùÊô²¿ÃŵÄÈËÔ±
<div style="text-align: center" mce_style="text-align: center" width="95%" class="tab">
<asp:DataList ID="DataList1" runat="server" Width="100%" RepeatDirection="vertical"
OnItemDataBound="DataList1_ItemDataBound" GridLines="Horizontal" RepeatLayout="table">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="chkDepart" runat="server" Text="" /></td>
</tr>
<tr>
<td style="text-align: left" mce_style="text-align: left" nowrap="nowrap">
<asp:Che ......
Return a formatted string
function sprintf ( ) {
// Return a formatted string
//
// version: 909.322
// discuss at: http://phpjs.org/functions/sprintf // + original by: Ash Searle (http://hexmen.com/blog/)
// + namespaced by: Michael White (http://getsprink.com)
// + tweaked by: Jack
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Paulo Ricardo F. Santos // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Brett Zamir (http://brett-zamir.me)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: sprintf("%01.2f", 123.1);
// * returns 1: 123.10 // * example 2: sprintf("[%10s]", 'monkey');
// * returns 2: '[ monkey]'
// * example 3: sprintf("[%'#10s]", 'monkey');
// * returns 3: '[####monkey]'
var regex = /%%|%(\d+\$)?([-+\'#0 ]*)(\*\d+\$|\*|\d+)?(\. ......
1. ==
JavascriptÓÐÁ½×éÏàµÈÔËËã·û£¬Ò»×éÊÇ==ºÍ!=£¬ÁíÒ»×éÊÇ===ºÍ!==¡£Ç°ÕßÖ»±È½ÏÖµµÄÏàµÈ£¬ºóÕß³ýÁËÖµÒÔÍ⣬»¹±È½ÏÀàÐÍÊÇ·ñÏàͬ¡£
Ç뾡Á¿²»ÒªÊ¹ÓÃǰһ×飬ÓÀԶֻʹÓÃ===ºÍ!==¡£ÒòΪ==ĬÈÏ»á½øÐÐÀàÐÍת»»£¬¹æÔòÊ®·ÖÄѼǡ£Èç¹ûÄã²»ÏàÐŵϰ£¬Çë»Ø´ðÏÂÃæÎå¸öÅжÏʽµÄÖµÊÇtrue»¹ÊÇfalse£º
¡¡¡¡false == 'false'
¡¡¡¡false == undefined
¡¡¡¡false == null
¡¡¡¡null == undefined
¡¡¡¡0 == ''
ǰÈý¸öÊÇfalse£¬ºóÁ½¸öÊÇtrue¡£
2. with
withµÄ±¾ÒâÊǼõÉÙ¼üÅÌÊäÈë¡£±ÈÈç
¡¡¡¡obj.a = obj.b;
¡¡¡¡obj.c = obj.d;
¿ÉÒÔ¼òд³É
¡¡¡¡with(obj) {
¡¡¡¡¡¡¡¡a = b;
¡¡¡¡¡¡¡¡c = d;
¡¡¡¡}
µ«ÊÇ£¬ÔÚʵ¼ÊÔËÐÐʱ£¬½âÊÍÆ÷»áÊ×ÏÈÅжÏobj.bºÍobj.dÊÇ·ñ´æÔÚ£¬Èç¹û²»´æÔڵϰ£¬ÔÙÅжÏÈ«¾Ö±äÁ¿bºÍdÊÇ·ñ´æÔÚ¡£ÕâÑù¾Íµ¼ÖÂÁ˵ÍЧÂÊ£¬¶øÇÒ¿ÉÄܻᵼÖÂÒâÍ⣬Òò´Ë×îºÃ²»ÒªÊ¹ÓÃwithÓï¾ä¡£
3. eval
evalÓÃÀ´Ö±½ÓÖ´ÐÐÒ»¸ö×Ö·û´®¡£ÕâÌõÓï¾äÒ²ÊDz»Ó¦¸ÃʹÓõģ¬ÒòΪËüÓÐÐÔÄܺͰ²È«ÐÔµÄÎÊÌ⣬²¢ÇÒʹµÃ´úÂë¸üÄÑÔĶÁ¡£
evalÄܹ»×öµ½µÄÊÂÇ飬²»ÓÃËüÒ²ÄÜ×öµ½¡£±ÈÈç
¡¡¡¡eval("myValue = myObject." + myKey + ";");
¿ÉÒÔÖ±½Óд³É
¡¡¡¡myValue = myObject[myKey];
ÖÁÓÚajax²Ù×÷·µ»ØµÄjson×Ö·û´®£ ......