jQuery¼¼Êõ

19 ¸ö JavaScript ÓÐÓõļòд¼¼Êõ Ç°¶Ë¿ª·¢£¬JQUERYÌØЧ£¬È«Õ»¿ª(2)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2018-02-15 10:33 ÎÒÒªÆÀÂÛ( )

JavaScriptÖÐÈç¹ûûÓÐÏòº¯Êý²ÎÊý´«µÝÖµ£¬Ôò²ÎÊýΪundefined¡£ÎªÁËÔöÇ¿²ÎÊý¸³Öµ£¬¿ÉÒÔʹÓÃifÓï¾äÀ´Å׳öÒì³££¬»òʹÓÃÇ¿ÖƲÎÊý¼òд·½·¨¡£ function foo(bar) { if(bar === undefined) {throw new Error('Missing param

JavaScriptÖÐÈç¹ûûÓÐÏòº¯Êý²ÎÊý´«µÝÖµ£¬Ôò²ÎÊýΪundefined¡£ÎªÁËÔöÇ¿²ÎÊý¸³Öµ£¬¿ÉÒÔʹÓÃifÓï¾äÀ´Å׳öÒì³££¬»òʹÓÃÇ¿ÖƲÎÊý¼òд·½·¨¡£

function foo(bar) { if(bar === undefined) { throw new Error('Missing parameter!'); } return bar; }

¼òд£º

mandatory = () => { throw new Error('Missing parameter!'); } foo = (bar = mandatory()) => { return bar; } Array.find¼òд

Ïë´ÓÊý×éÖвéÕÒij¸öÖµ£¬ÔòÐèҪѭ»·¡£ÔÚES6ÖУ¬find()º¯ÊýÄÜʵÏÖͬÑùЧ¹û¡£

<

pre>
const pets = [
{ type: 'Dog', name: 'Max'},
{ type: 'Cat', name: 'Karl'},
{ type: 'Dog', name: 'Tommy'},
]

function findDog(name) {
for(let i = 0; i<pets.length; ++i) {
if(pets[i].type === 'Dog' && pets[i].name === name) {
return pets[i];
}
}
}

¼òд£º

pet = pets.find(pet => pet.type ==='Dog' && pet.name === 'Tommy'); console.log(pet); // { type: 'Dog', name: 'Tommy' } Object[key]¼òд

¿¼ÂÇÒ»¸öÑéÖ¤º¯Êý

function validate(values) { if(!values.first) return false; if(!values.last) return false; return true; } console.log(validate({first:'Bruce',last:'Wayne'})); // true

¼ÙÉèµ±ÐèÒª²»Í¬ÓòºÍ¹æÔòÀ´ÑéÖ¤£¬ÄÜ·ñ±àдһ¸öͨÓú¯ÊýÔÚÔËÐÐʱȷÈÏ£¿

// ¶ÔÏóÑéÖ¤¹æÔò const schema = { first: { required:true }, last: { required:true } } // ͨÓÃÑéÖ¤º¯Êý const validate = (schema, values) => { for(field in schema) { if(schema[field].required) { if(!values[field]) { return false; } } } return true; } console.log(validate(schema, {first:'Bruce'})); // false console.log(validate(schema, {first:'Bruce',last:'Wayne'})); // true

ÏÖÔÚ¿ÉÒÔÓÐÊÊÓÃÓÚ¸÷ÖÖÇé¿öµÄÑéÖ¤º¯Êý£¬²»ÐèҪΪÁËÿ¸ö¶ø±àд×Ô¶¨ÒåÑéÖ¤º¯ÊýÁË

Ë«ÖØ·ÇλÔËËã¼òд

ÓÐÒ»¸öÓÐЧÓÃÀýÓÃÓÚË«ÖØ·ÇÔËËã²Ù×÷·û¡£¿ÉÒÔÓÃÀ´´úÌæMath.floor()£¬ÆäÓÅÊÆÔÚÓÚÔËÐиü¿ì£¬¿ÉÒÔÔĶÁ´ËÎÄÕÂÁ˽â¸ü¶àλÔËËã¡£

Math.floor(4.9) === 4 //true

¼òд£º

~~4.9 === 4 //true

±¾ÎÄ·­ÒëÓÚSitePoint£º

Ô­ÎĵØÖ·£ºhttps://segmentfault.com/a/1190000012673854

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • Jquery¸ßЧ±à³Ì¼¼ÇÉ

    Jquery¸ßЧ±à³Ì¼¼ÇÉ

    2018-02-13 16:03

  • JavaScript»ñÈ¡Ñ¡ÖÐÎı¾ ¹â±êËùÔÚÔªËØ

    JavaScript»ñÈ¡Ñ¡ÖÐÎı¾ ¹â±êËùÔÚÔªËØ

    2018-02-12 17:06

  • ¡¾jQuery½Ì³Ì¡¿¡¶¾«Í¨Javascript+jQuery¡·ÊÓƵ½Ì³ÌĿ¼ - jQuery - ¡£

    ¡¾jQuery½Ì³Ì¡¿¡¶¾«Í¨Javascript+jQuery¡·ÊÓƵ½Ì³ÌĿ¼ - jQuery -

    2018-02-12 09:06

  • jquery-plugin-simplemodalѧϰ

    jquery-plugin-simplemodalѧϰ

    2018-02-11 10:00

ÍøÓѵãÆÀ
÷