<span id="Label3"></p><p><p>Apply,call should be our more familiar approach, like Math.max.apply (arr), taking the maximum value in an array element, Array.prototype.slice.call (obj) The use of apply and call has been explained by changing obj into an array.</p></p><p><p>There is no difference in performance between the two, except that there is a certain difference in the parameters, the arguments of apply are in the form of arrays, and call is the form of a single element, for example, the most common add.call (sub,3,1) we see on the internet is the successful invocation of the add method, Get Results 4. of course, The call method can also be implemented in the function of similar multiple inheritance problems on the internet also have similar answers, no longer listed.</p></p><p><p>So bind, is a function that we often use in the jquery framework to bind certain events, and in JavaScript we seldom see the use of this, and today take this out for speculation because of the fact that a more interesting topic came to this method.</p></p><p><p>The topics Are:</p></p><p><p></p></p><p style="margin-top:0px; margin-bottom:16px; padding-top:0px; padding-bottom:0px; font-size:14px; direction:ltr; font-family:Lato,‘Helvetica Neue‘,Helvetica,sans-serif; line-height:22.3999996185303px"><p style="margin-top:0px; margin-bottom:16px; padding-top:0px; padding-bottom:0px; font-size:14px; direction:ltr; font-family:Lato,‘Helvetica Neue‘,Helvetica,sans-serif; line-height:22.3999996185303px"><span style="background-color:rgb(255,255,255)"><span style="color:#efefef"> </span> <span style="color:#330000">Extend the dictionary with a method, that returns a list of words matching a pattern. This pattern may contain letters (lowercase) and placeholders ("?"). A placeholder stands for exactly one arbitrary letter.</span></span></p></p><p style="margin-top:0px; margin-bottom:16px; padding-top:0px; padding-bottom:0px; font-size:14px; direction:ltr; font-family:Lato,‘Helvetica Neue‘,Helvetica,sans-serif; line-height:22.3999996185303px"><p style="margin-top:0px; margin-bottom:16px; padding-top:0px; padding-bottom:0px; font-size:14px; direction:ltr; font-family:Lato,‘Helvetica Neue‘,Helvetica,sans-serif; line-height:22.3999996185303px"><span style="background-color:rgb(255,255,255)"><span style="color:#330000">Javascript Example:</span></span></p></p><pre lang="javascript" style="margin-top:0px; margin-bottom:0px; padding:0px; direction:ltr"><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><span style="background-color:rgb(255,255,255)"><span style="background-color:rgb (255,255,255)"><span style="color: #330000"><span class="hljs-keyword" style="">var </span> fruits = <span class="hljs-keyword" style=">new </span> Dictionary ([<span class=" hljs-string "style=" ">" Banana ' </span>, <span class="hljs-string" style=""> ' Apple ' </span>, <span class="hljs-string" style=""> Papaya ' </span>, <span class="hljs-string" style=""> ' cherry ' </span>]); fruits.getmatchingwords ( <span class="hljs-string" style=""> ' Lemon ' </span>); <span class="hljs-comment" style="">//must return [] </span> fruits.getmatchingwords ( <span class="hljs-string" style=""> "cherr?? ' </span>); <span class="hljs-comment" style="">//must return [] </span> fruits.getmatchingwords ( <span class="hljs-string" style=""> "a?a?a ' </span>); <span class="hljs-comment" style="">//must return [' banana ', ' papaya '] </span> fruits.getmatchingwords (<span class=" hljs-string " style=" "> '?????? ' </span>); <span class="hljs-comment" style="">//must return [' banana ', ' papaya ', </span> </span> </span></span></code><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><span style="background-color:rgb(255,255,255)"><span style="color:#330000"><span class="hljs-comment" style="">那么这种题目应该怎么样求解呢。我起初想到的是要是可以利用数组中的元素在正则表达式?的位置任意替换就可以了。</span></span></span></code></code><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><span style="background-color:rgb(255,255,255)"><span style="color:#330000"><span class="hljs-comment" style="">那么这个正则表达式应该怎么写呢。</span></span></span></code></code><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><span style="background-color:rgb(255,255,255)"><span style="color:#330000"><span class="hljs-comment" style=""> pattern = new RegExp(‘^‘+pattern.replace(/\?/g, ‘.‘)+‘$‘); 这个表达式的意思是将?的位置替换成任意字符。</span></span></span></code></code><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><span style="color:#330000"><span style="background-color:rgb(255,255,255)">然后呢,我没辙了,这个正则表达式求出来应该怎么用,我没有想法,所以看了一下推荐最高的回答,其中发现了bind的踪影,它的解答为:</span></span></code></code><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><span style="color:#330000"><span style="background-color:rgb(255,255,255)"> return this.words.filter(pattern.test.bind(pattern)); 其中this.words是dictionary构造函数中的参数。</span></span></code></code><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><span style="color:#330000"><span style="background-color:rgb(255,255,255)">然后发现javascript里面的bind的意思和apply类似,却又不同,它可以改变this的指向,而且起到了函数的作用,而而apply,call则不是,譬如我们是可以这样。</span></span></code></code><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><span style="color:#330000"><span style="background-color:rgb(255,255,255)"><span style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; font-size:14px; line-height:25.2000007629395px; background-color:rgb(221,237,251)"><span style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; font-size:14px; line-height:25.2000007629395px; background-color:rgb(221,237,251)">var Anotherguysayhello = person.sayHello.bind ({</span></span><br style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; font-size:14px; line-height:25.2000007629395px"><span style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; font-size:14px; line-height:25.2000007629395px; background-color:rgb(221,237,251)"><span style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; font-size:14px; line-height:25.2000007629395px; background-color:rgb(221,237,251)">name: ' Alex ',</span></span><br style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; font-size:14px; line-height:25.2000007629395px"><span style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; font-size:14px; line-height:25.2000007629395px; background-color:rgb(221,237,251)"><span style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; font-size:14px; line-height:25.2000007629395px; background-color:rgb(221,237,251)">job: ' back end C # Developer '</span></span><br style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; font-size:14px; line-height:25.2000007629395px"><span style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; font-size:14px; line-height:25.2000007629395px; background-color:rgb(221,237,251)"><span style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; font-size:14px; line-height:25.2000007629395px; background-color:rgb(221,237,251)">}); To define a function like this, you can call Anotherguysayhello directly. but instead of calling and apply, you can't. </span></span></span></span></code><code style="overflow-x:auto; display:block; padding:10px; margin-bottom:15px; margin-top:0px"><code style="overflow-x:auto; display:block; padding:10px; margin-bottom:15px; margin-top:0px"><span style="font-family:Tahoma,Helvetica,Arial,宋体,sans-serif; color:#330000"><span style="font-size:14px; line-height:25.2000007629395px; background-color:rgb(221,237,251)">可是,我对于pattern.test.bind(pattern)怎么使用得到true和false还有一点点迷茫,所以大家如果对这个题目有自己的理解就帮忙留言一下啦。</span></span></code></code><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><span style="color:#330000"><span style="background-color:rgb(255,255,255)"></span></span></code></code><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><code style="font-size:13px; line-height:22.3999996185303px; overflow-x:auto; display:block; padding:10px; font-family:CamingoCode-Regular,monospace; margin-bottom:15px; margin-top:0px"><span style="background-color:rgb(255,255,255)"><span style="color:#330000"><span class="hljs-comment" style=""></span></span></span></code></code><p style="margin-top:0px; margin-bottom:16px; padding-top:0px; padding-bottom:0px; font-size:14px; direction:ltr; font-family:Lato,‘Helvetica Neue‘,Helvetica,sans-serif; line-height:22.3999996185303px; color:rgb(239,239,239)"><p style="margin-top:0px; margin-bottom:16px; padding-top:0px; padding-bottom:0px; font-size:14px; direction:ltr; font-family:Lato,‘Helvetica Neue‘,Helvetica,sans-serif; line-height:22.3999996185303px; color:rgb(239,239,239)"></p></p></pre><br><p><p>Apply,call,bind methods in JavaScript</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service