ÔÚÕâÀÁíÒ»ÖÖÖصãÊÇ£¬Õ¹Ê¾ÄãÒâʶµ½ÈçºÎÔÚ²»ÖØд¿ÉÄÜÒѾ¶¨ÒåµÄº¯Êý¡£Õâ¸öÐèÒªÔÚ×Ô¶¨Ò庯Êý֮ǰÅжϺ¯Êý²»´æÔÚ¡£
`String.prototype.repeatify = String.prototype.repeatify || function(times) {/* code here */};``µ±Äã±»ÒªÇóÒ»¸öµæƬjavascriptº¯Êýʱ£¬Õâ¸ö¼¼ÇÉÊ®·ÖÓÐÓá£
ÎÊÌâ3: ÌáÉý±äÁ¿£¨Hoisting£©Ö´ÐÐÒÔÏ´úÂë»áÓÐʲô½á¹û£¿ÎªÊ²Ã´»áÕâÑù£¿
function test() { console.log(a); console.log(foo()); var a = 1; function foo() { return 2; } } test();´ð°¸Õâ¶Î´úÂëµÄÖ´Ðнá¹ûÊÇ undefined ºÍ 2 ¡£
Õâ¸ö½á¹ûµÄÔÒòÊÇ£¬±äÁ¿ºÍº¯Êý¶¼±»ÌáÉýÁË£¨ hoisted £©¡£Òò´Ë£¬ÄǸöʱºò a ±»´òÓ¡ÁË£¬Ëü´æÔÚº¯ÊýÖ®ÖУ¨Ò²¾ÍÊÇÉùÃ÷ÁË£©£¬µ«ÊÇÒÀÈ»ÊÇ undefined ¡£»»ÑÔÖ®£¬ÉÏÊö´úÂëºÍÒÔÏ´úÂëÊÇÏàͬµÄ¡£
function test() { var a; function foo() { return 2; } console.log(a); console.log(foo()); a = 1; } test();ÎÊÌâ4: this ÔÚjavascriptÖÐÊÇÈçºÎ¹¤×÷µÄÒÔÏ´úÂëµÄ½á¹ûÊÇʲô£¿Çë½âÊÍÄãµÄ´ð°¸¡£
var fullname = 'John Doe'; var obj = { fullname: 'Colin Ihrig', prop: { fullname: 'Aurelio De Rosa', getFullname: function() { return this.fullname; } } }; console.log(obj.prop.getFullname()); var test = obj.prop.getFullname; console.log(test());´ð°¸´úÂë´òÓ¡ÁË Aurelio De Rosa ºÍ John Doe ¡£ÔÒòÊÇÔÚjavascriptÖУ¬Ò»¸öº¯ÊýµÄÓï¾³£¬Ò²¾ÍÊÇ this Õâ¸ö¹Ø¼ü´ÊÒýÓõģ¬ÒÀÀµÓÚº¯ÊýÊÇÈçºÎµ÷Óõģ¬²»ÊÇÈçºÎ¶¨ÒåµÄ¡£
ÔÚµÚÒ»¸ö console.log() µ÷ÓÃÖУ¬ getFullname() ÊÇ×÷Ϊ obj.prop µÄº¯Êý±»µ÷Óõġ£Òò´Ë£¬ÕâÀïµÄÓï¾³Ö¸ÏòºóÕß²¢ÇÒº¯Êý·µ»Ø¶ÔÏóµÄ fullname ÊôÐÔ¡£Ïà·´£¬µ± getFullname() ±»Ö¸¶¨Îª test µÄ±äÁ¿£¬ÄǸöÓï¾³Ö¸ÏòÈ«¾Ö¶ÔÏó( window )¡£ÒòΪ test Ï൱ÓÚÉèÖÃΪȫ¾Ö¶ÔÏóµÄÊôÐÔ¡£ÒòΪÕâ¸öÔÒò£¬º¯Êý·µ»Ø window µÄÒ»¸ö fullname ÊôÐÔ£¬ÕâÔÚÕâ¸ö°¸ÀýÖÐÊÇÔÚ´úÂëƬ¶ÎÖеÚÒ»ÐÐÉèÖõġ£
ÎÊÌâ5: call() ºÍ apply()ÐÞ¸´ÉÏÒ»¸öÎÊÌ⣬ÈÃ×îºóÒ»¸ö console.log() ´òÓ¡³ö Aurelio De Rosa ¡£
´ð°¸Õâ¸öÎÊÌâ¿ÉÒÔͨ¹ýº¯Êý call() »òÕß apply() Ç¿Öƺ¯ÊýÓï¾³¡£Èç¹ûÄã²»ÖªµÀ call() ºÍ apply() Ö®¼äµÄÇø±ð£¬ÎÒÍƼöÔĶÁÎÄÕ function.callºÍfunction.applyÖ®¼äÓкÍÇø±ð? ¡£ÔÚÒÔÏ´úÂëÖУ¬ÎÒ»áÓà call() £¬µ«ÊÇÔÚÕâ¸ö°¸ÀýÖУ¬ apply() Ò²¿ÉÒÔ»ñµÃÏàͬµÄ½á¹û£º
`console.log(test.call(obj.prop));``½áÂÛÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃÇÌÖÂÛÁË,ÔÚÃæÊÔÖÐΪÁ˲âÊÔjavascript¿ª·¢Õߣ¬¶ø»áÎʵ½µÄÎåÖÖµäÐ͵ÄÎÊÌâ¡£À´×ÔÃæÊÔµÄÕæʵÎÊÌâ¿ÉÄÜ»áÓÐËù²»Í¬£¬µ«ÊǺ¸ÇµÄ¸ÅÄîºÍÖ÷Ìâͨ³£¶¼ÊÇÊ®·ÖÏàËƵġ£ÎÒÏ£ÍûÄãÓäÔõزâÊÔÄãµÄÄÜÁ¦¡£ÍòÒ»Äã²»ÖªµÀËùÓеĴ𰸣¬²»Òªµ£ÐÄ£ºÃ»ÓÐѧϰºÍ¾Ñé²»Äܽâ¾öµÄÎÊÌâ¡£
Èç¹ûÄãÔÚÃæÊÔÖб»Îʵ½ÁËÆäËûÓÐȤµÄÎÊÌ⣬²»ÒªÓÌÔ¥ÂíÉÏÀ´ºÍÎÒÃÇ·ÖÏí°É¡£Õâ»á°ïÖúµ½ºÜ¶àµÄ¿ª·¢Õß¡£
ÒÔÉϾÍÊÇÎåµÀµäÐ͵ÄjavascriptÃæÊÔÌâµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢phpÖÐÎÄÍøÆäËüÏà¹ØÎÄÕ£¡
¼Ì³ÐµÄ±¾ÖÊ£ºÖØÓÃ
ÔÚ̽ÌÖ javaScript µÄÔÐͼ̳Ð֮ǰ£¬ÏȲ»·ÁÏëÏëΪʲôҪ¼Ì³Ð£¿
¿¼ÂÇÒ»¸ö³¡¾°£¬Èç¹ûÎÒÃÇÓÐÁ½¸ö¶ÔÏó£¬ËüÃÇÒ»²¿·ÖÊôÐÔÏàͬ£¬ÁíÒ»²¿ÊôÐÔ²»Í¬¡£Í¨³£Ò»¸öºÃµÄÉè¼Æ·½°¸Êǽ«ÏàͬÂß¼³é³öÀ´£¬ÊµÏÖÖØÓá£
ÒÔ xiaoMing liLei Á½Î»Í¬Ñ§¾ÙÀý¡£ÕâÁ½Î»Í¬Ñ§ÓÐ×Ô¼ºµÄÃû×Ö£¬²¢ÇÒ»á½éÉÜ×Ô¼º¡£³éÏóΪ³ÌÐò¶ÔÏ󣬿ÉÒÔ×öÈçϱíʾ¡£
var xiaoMing = { name : "xiaoMing", hello : function(){ console.log( 'Hello, my name is '+ this.name + '.'); } } var liLei = { name : "liLei", hello : function(){ console.log( 'Hello, my name is '+ this.name + '.'); } }ʹÓùý java µÄͬѧ£¬¿ÉÄܵÚÒ»ÑÛ¾ÍÏëµ½ÁËÓÃÃæÏò¶ÔÏóÀ´½â¾öÕâ¸öÎÊÌâ¡£´´ÔìÒ»¸ö Person µÄÀ࣬ȻºóʵÀý»¯ xiaoMing ºÍ liLei Á½¸ö¶ÔÏó¡£ÔÚ ES6 ÖÐÒ²ÓÐÀàËÆÓÚ java ÖÐÀàµÄ¸ÅÄ class ¡£
ÏÂÃæʹÓà ES6 µÄÓï·¨£¬ÓÃÃæÏò¶ÔÏóµÄ˼·À´Öع¹ÉÏÃæµÄ´úÂë¡£
class Person { constructor(name){ this.name = name } hello(){ console.log(this.name); } } var xiaoMing = new Person('xiaoMing'); var liLei = new Person('liLei');¿ÉÒÔ¿´µ½£¬Ê¹ÓÃÀà´´½¨¶ÔÏ󣬴ﵽÁËÖØÓõÄÄ¿µÄ¡£Ëü»ùÓÚµÄÂß¼ÊÇ£¬Á½¸ö»ò¶à¸ö¶ÔÏóµÄ½á¹¹¹¦ÄÜÀàËÆ£¬¿ÉÒÔ³éÏó³öÒ»¸öÄ£°å£¬ÒÀÕÕÄ£°å¸´ÖƳö¶à¸öÏàËƵĶÔÏó¡£
ʹÓÃÀà´´½¨¶ÔÏ󣬾ÍÏñ×ÔÐгµÖÆÔìÉÌÒ»±éÒ»±éµØÖØÓÃÏàͬµÄÀ¶Í¼À´ÖÆÔì´óÁ¿µÄ×ÔÐгµ¡£
È»½â¾öÖØÓÃÎÊÌâµÄ·½°¸£¬µ±È»²»Ö¹Ò»ÖÖ¡£´«Í³ÃæÏò¶ÔÏóµÄÀֻ࣬ÊÇÆäÖеÄÒ»ÖÖ·½°¸¡£ÏÂÃæÂÖµ½ÎÒÃǵÄÖ÷½Ç¡°ÔÐͼ̳С±µÇ³¡ÁË£¬Ëü´ÓÁíÒ»¸ö½Ç¶È½â¾öÁËÖØÓõÄÎÊÌâ¡£
ÔÐͼ̳еÄÔÀíÔÐͶÔÏójavaScript ÖÐµÄ object ÓÉÁ½²¿·Ö×é³É£¬ÆÕͨÊôÐԵļ¯ºÏ£¬ºÍÔÐÍÊôÐÔ¡£
var o = { a : 'a', ... proto: prototypeObj }ÆÕͨÊôÐÔÖ¸µÄ¾ÍÊÇ a £» ÔÐÍÊôÐÔ Ö¸µÄÊÇ proto ¡£Õâ±¾²»ÊôÓڹ淶µÄÒ»²¿·Ö£¬ºóÀ´ chrome ͨ¹ý proto ½«Õâ¸öÓïÑԵײãÊôÐÔ¸ø±©Â¶³öÀ´ÁË£¬ÂýÂýµÄ±»´ó¼ÒËù½ÓÊÜ£¬Ò²¾ÍÌí¼Óµ½ ES6 ¹æ·¶ÖÐÁË¡£ o.proto µÄÖµ prototypeObj Ò²¾ÍÊÇ ÔÐͶÔÏó ¡£ÔÐͶÔÏóÆäʵҲ¾ÍÊÇÒ»¸öÆÕͨ¶ÔÏó£¬Ö®ËùÒÔ½ÐÔÐͶÔÏóµÄÔÒò£¬Ö»ÊÇÒòΪËüÊÇÔÐÍÊôÐÔËùÖ¸µÄÖµ¡£
ÔÐͶÔÏóËùÒÔÌØÊ⣬ÊÇÒòΪËüÓµÓÐÒ»¸öÆÕͨ¶ÔÏóûÓеÄÄÜÁ¦£º½«ËüµÄÊôÐÔ¹²Ïí¸øÆäËû¶ÔÏó¡£
ÔÚ ES6 ¹æ·¶ ÖУ¬¶ÔËüÊÇÈç϶¨ÒåµÄ£º
object that provides shared properties for other objectsÊôÐÔ¶Á²Ù×÷»Øµ½×ʼµÄÀý×Ó£¬¿´¿´ÈçºÎÀûÓÃÔÐͼ̳ÐʵÏÖÖØÓõÄÄ¿µÄ¡£
var prototypeObj = { hello: function(){ console.log( 'Hello, my name is '+ this.name + '.'); } // ... } var xiaoMing = { name : "xiaoMing", proto : prototypeObj } var liLei = { name : "liLei", proto : prototypeObj } xiaoMing.hello(); // Hello, my name is xiaoMing. liLei.hello(); // Hello, my name is liLei.xiaoMing liLei ¶ÔÏóÉÏ£¬²¢Ã»Ö±½ÓÓµÓÐ hello ÊôÐÔ(·½·¨)£¬µ«ÊÇÈ´ÄܶÁÈ¡¸ÃÊôÐÔ(Ö´Ðи÷½·¨)£¬ÕâÊÇΪʲô£¿
ÏëÏóÒ»¸ö³¡¾°£¬ÄãÔÚ×öÊýѧ×÷Òµ£¬Óöµ½Ò»¸öºÜÄѵÄÌâÄ¿£¬Äã²»»á×ö¡£¶øÄãÓÐÒ»¸öºÃÐֵܣ¬ÊýѧºÜÀ÷º¦£¬ÄãÈ¥Çë½ÌËû£¬°ÑÕâµÀÌâ×ö³öÀ´ÁË¡£
xiaoMing ¶ÔÏóÉÏ£¬Ã»ÓÐ hello ÊôÐÔ£¬µ«ÊÇËüÓÐÒ»¸öºÃÐֵܣ¬ prototypeObj ¡£ÊôÐÔ¶Á²Ù×÷£¬ÔÚ xiaoMing ÉíÉÏûÓÐÕÒµ½ hello ÊôÐÔ£¬¾Í»áÈ¥ÎÊËüµÄÐÖµÜ prototypeObj ¡£ËùÒÔ hello ·½·¨»á±»Ö´ÐС£
ÔÐÍÁ´»¹ÊÇ×öÊýѧÌâµÄÀý×Ó¡£ÄãµÄÊýѧÌâÄ¿ºÜÄÑ£¬ÄãµÄÐÖµÜҲûÓд𰸣¬ËûÍƼöÄãÈ¥ÎÊÁíÍâÒ»¸öͬѧ¡£ÕâÑùÖ±µ½ÓÐÁ˴𰸻òÕßÔÙҲûÓÐÈË¿ÉÒÔÎÊ£¬Äã¾Í²»»áÔÙÎÊÏÂÈ¥¡£ÕâÑù¾ÍºÃÏñÓÐÒ»ÌõÎÞÐÎÁ´Ìõ°ÑÄãºÍͬѧÃÇÇ£ÔÚÁËÒ»Æð¡£
¡¡