当前位置: 首页 > 编程日记 > 正文

JavaScript的“ this”通过成立一个高中乐队来解释

by Kevin Kononenko

凯文·科诺年科(Kevin Kononenko)

JavaScript的“ this”通过成立一个高中乐队来解释 (JavaScript’s “this” Explained By Starting A High School Band)

If you have ever been in a band, had a friend that started a band, or seen a corny 80s movie about starting a band, then you can understand the concept of “this” in JavaScript.

如果您曾经参加过乐队,或者有朋友开始过乐队,或者看过关于80年代老套的电影,那么您可以理解JavaScript中“ this”的概念。

When you are reading over some JavaScript, and you come across the this keyword, the steps you need to take in order to figure out its value might seem obvious.

当您阅读一些JavaScript时,遇到了this关键字,为了弄清它的值而需要采取的步骤似乎很明显。

You might be thinking, “I just need to find the function that contains this, and then I will know what it is referring to!”

您可能会想:“我只需要找到包含this的函数,然后我就知道它指的是什么!”

let band= {  name: "myBand",  playGig:function() {    console.log("Please welcome to the stage" + this.name);  }}

In the example above, for example, this.name refers to the name “myBand”. This seems easy!

例如,在上面的示例中, this.name引用名称“ myBand”。 这似乎很简单!

But, as you learn more JavaScript concepts, like closures and callbacks, you will quickly find that this does not behave like you would expect.

但是,当你学习更多JavaScript概念,如封锁和回调,你很快就会发现, 不会表现得像你所期望的。

So, I wanted to create a visual explanation of how this works in JavaScript. Here’s the scenario: You are back in high school, and starting a band with your friends (or maybe you are currently in high school?)

所以,我想创造的是如何工作JavaScript的可视化解释。 情况是这样的:您回到了高中,然后和您的朋友一起开始了乐队演出(或者您可能正在上高中?)

  • Your band has four members

    您的乐队有四名成员
  • You play three types of gigs: you play at bars, school competitions, and public events in town.

    您会演奏三种类型的演奏:在酒吧,学校比赛和镇上的公共活动中玩。
  • Your team can play all types of music, so you try to choose the right songs to match the audience. You don’t want curse words or sexual references at the family-friendly events, for example.

    您的团队可以播放所有类型的音乐,因此您尝试选择合适的歌曲以匹配听众。 例如,您不希望在家庭友善活动中骂人或性提及他人。

As you will soon see, the biggest concept you need to understand with this is execution context. That is what determines the value of this.

正如你很快就会看到,你需要方面的了解最大的概念是执行上下文。 那就是决定this的价值的原因

Before you use this tutorial, you need to understand objects and variables. Check out my tutorials on each of these subjects if you need to review.

在使用本教程之前,您需要了解对象和变量 。 如果需要复习,请查看我关于这些主题的教程。

If you are interested in a more technical version of this tutorial, check out the guide from JavaScriptIsSexy.

如果您对本教程的更多技术版本感兴趣,请查看JavaScriptIsSexy的指南 。

全局执行上下文 (The Global Execution Context)

Let’s say that your band needs to do a family-friendly gig at the local park as part of a local fair. You need to choose the right type of music that will keep parents happy and also not offend anyone else.

假设您的乐队需要在当地公园做家庭友好的演出,这是当地集市的一部分。 您需要选择正确的音乐类型,使父母保持快乐,同时又不会冒犯其他任何人。

Let’s say that you choose to play some songs by Billy Joel (a famous American artist), and even though this is not your favorite, you know that it’s what you need to do to get paid.

假设您选择播放比利·乔尔 ( Billy Joel,美国著名艺术家)的一些歌曲,即使这不是您的最爱,您也知道要获得报酬才需要这样做。

Here is what that looks like in code.

这就是代码中的样子。

//The songs you will play var artist= "Billy Joel";
function playGig(){   //instruments that your band will use   let instruments= ["piano", "microphone", "acousticGuitar", "harmonica"];
console.log("We are going to be playing music from " + this.artist + "tonight!"); }
playGig();

In the example above, we have an artist variable that indicates what type of music we will be playing. And we have an array full of instruments that will be used to play that music within the playGig function.

在上面的示例中,我们有一个artist 变量 ,该变量指示我们将播放的音乐类型。 我们有各种各样的乐器 ,可用于在playGig 函数中播放音乐。

In the last line, we call the playGig function. So what is this.artist, in this case?

在最后一行,我们调用playGig函数。 那么,在这种情况下, this.artist是什么?

Well, first we must determine the execution context for this function. The execution context is determined by the object that the function is called upon.

好吧,首先我们必须确定该函数的执行上下文 。 执行上下文由调用函数对象确定。

In this case, there is no object listed, so that means that the function is called on the window object. It could also be called like this:

在这种情况下,没有列出对象,这意味着在窗口对象上调用了该函数。 也可以这样称呼:

window.playGig(); "We are going to be playing music from Billy Joel tonight!"

This is the global execution context. The function is called at the level of the global object, window. And, the variable artist is available as a property of the window object (see this note on the JavaScript specification).

这是全局执行上下文 。 该函数在全局对象window级别调用。 并且,变量artist窗口对象的属性( 请参见JavaScript规范上的此注释 )。

So, in line 1 of the snippet above, we are also saying:

因此,在上面的代码段的第一行中,我们还说:

//old version- let artist = "Billy Joel"; this.artist="Billy Joel";

Your band is executing the gig on the global context by playing music that appeals to everyone (unless there are any Billy Joel haters out there).

您的乐队正在通过播放吸引所有人的音乐在全球范围内进行演出(除非那里有Billy Joel讨厌的人)。

对象级执行上下文 (Object-Level Execution Context)

Let’s say that your band got a gig at a local bar. This is great! Now, you don’t need to play music that satisfies everyone in town. You only need to play music that people can dance to.

假设您的乐队在当地一家酒吧演出。 这很棒! 现在,您无需播放满足城镇所有人需求的音乐。 您只需要播放人们可以跳舞的音乐。

Let’s say that you choose Coldplay, since most of their recent songs are pop music. You need a piano, microphone, drum set, and guitar for this gig.

假设您选择Coldplay ,因为它们的大多数最新歌曲都是流行音乐。 您需要钢琴,麦克风,架子鼓和吉他来进行演出。

Let’s create a bar object with the same pattern as we created for the public park gig.

让我们创建一个与为公园演出创建的图案相同的酒吧对象。

//The songs you will play in the public park/fair var artist= "Billy Joel";
function playGig(){   //instruments that your band will use   let instruments= ["piano", "microphone", "acousticGuitar", "harmonica"];   console.log("We are going to be playing music from " + this.artist + "tonight!"); }
//NEW PART let bar = {  artist:"coldplay",  playGig: function(){     //instruments that your band will use     let instruments= ["piano", "microphone", "guitar", "drumset"];         console.log("We are going to be playing music from " + this.artist + "tonight!");    } }

Here is the diagram for the code above:

这是上面的代码图:

So, let’s say that we want to write the code to get the gig at the bar started. We need to watch our execution context, which is the bar object in this case. Here is what that would look like:

因此,假设我们要编写代码以开始演奏会。 我们需要注意执行上下文 ,在这种情况 ,它是bar对象。 看起来像这样:

bar.playGig(); //"We are going to be playing music from coldplay tonight!"

And, we can still execute the playGig function on a global level — but we will get a different output. This is great news, since we do not want to be playing Billy Joel or Coldplay at the wrong venue…

而且,我们仍然可以在全局级别执行playGig函数-但我们将获得不同的输出。 这是个好消息,因为我们不想在错误的场地上玩比利·乔尔或酷玩乐队…

playGig(); //"We are going to be playing music from Billy Joel tonight!"

So far, this has been the easy stuff. Whenever we have been calling a function, the object that provides the execution context has been pretty straightforward. But that is about to change as we get more complex.

到目前为止,这一直很简单。 每当我们调用一个函数时,提供执行上下文的对象就非常简单。 但是随着我们变得越来越复杂,这将改变。

使用jQuery更改执行上下文 (Changing Execution Context using jQuery)

It’s the big event that has been covered in every single movie from the 1980s: The Battle of The Bands! Yes, every band in your high school is going to get into a competition to see who is the best.

这是1980年代的每部电影都涵盖的重大事件:乐队之战! 是的,您高中的每个乐队都将参加比赛,看看谁是最好的。

You are going to play some songs from AC/DC, pretty much the coolest band on the planet. But in order to do that, you need a different instrument mix than before:

您将要播放AC / DC上的一些歌曲,这几乎是地球上最酷的乐队。 但是为了做到这一点,您需要与以前不同的乐器组合:

  • A microphone

    麦克风
  • An electric guitar

    电吉他
  • A bass guitar

    低音吉他
  • A drumset

    架子鼓

Let’s call this the battle object. Here is what it looks like in code.

我们称其为战斗对象 。 这就是代码中的样子。

let battle = {  artist:"acdc",  playGig: function(){     //instruments that your band will use     let instruments= ["microphone", "electricguitar", "bass", "drumset"];
console.log("We are going to be playing music from " + this.artist + "tonight!");   } }

Since this is an annual event, we are going to use a click event from jQuery to start your show. Here is what that looks like:

由于这是一项年度活动,因此我们将使用jQuery的click 事件来开始您的表演。 看起来像这样:

$('#annualBattle').click(battle.playGig);

But if you actually ran this code… it would not work. Your band would forget the words and the notes, then slowly walk off the stage.

但是,如果您实际上运行了此代码,它将无法正常工作。 您的乐队会忘记单词和音符,然后慢慢离开舞台。

To figure out why, let’s return to execution context. We are referencing a DOM element called #annualBattle, so let’s see where that fits within the window object.

为了弄清楚原因,让我们回到执行上下文。 我们正在引用一个名为#annualBattle的DOM元素,所以让我们看一下它在window对象中的适合位置。

Since #annualBattle is an element in the DOM, it is part of the document object within the window object. It doesn’t have any property called artist. So if you ran the code, you would get:

由于#annualBattle是DOM中的元素,因此它是window对象内文档对象的一部分。 它没有任何称为artist的属性。 因此,如果运行代码,您将获得:

$('#annualBattle').click(battle.playGig); //"We are going to be playing music from undefined tonight!"

In this case, the execution context is an element from the DOM. That is what kicked off the click() method, which used the playGig function as a callback. So, this will end up with an undefined value.

在这种情况下, 执行上下文是DOM中的元素。 这就是click()方法的开始,该方法使用playGig函数作为回调 。 因此, 将以不确定的值结束。

In our analogy, this means that your band showed up to the competition with all their instruments, got in position to play, and then stared at the crowd like it was going to tell them what to do. It means you have forgotten the context of why you were there in the first place.

以我们的类比来说,这意味着您的乐队参加了所有乐器的比赛,可以演奏,然后凝视人群,就像要告诉他们该怎么做。 这意味着您首先忘记了为什么要呆在那里的背景。

To solve this, we need to use the bind() method to make sure that the playGig method still references the battle object, even when we call it from the context of a different object! It looks like this:

为了解决这个问题,我们需要使用bind()方法来确保playGig方法仍然引用战斗对象,即使从另一个对象的上下文调用它也是如此! 看起来像这样:

$('#annualBattle').click(battle.playGig.bind(battle)); //"We are going to be playing music from acdc tonight!"

Now, we get the correct output, even though the context was a DOM element.

现在,即使上下文是DOM元素,我们也能获得正确的输出。

使功能脱离上下文 (Pulling A Function Out of Context)

Let’s say that we wanted to write the code that will allow us to practice for the Battle of the Bands event. We will create a separate variable called practice, and assign the playGig method from the battle object.

假设我们想编写代码,使我们能够练习乐队战斗。 我们将创建一个名为实践单独的变量,并指定由战斗对象的playGig 方法

var artist= "Billy Joel";
function playGig(){  //instruments that your band will use   let instruments= ["piano", "microphone", "acousticGuitar", "harmonica"];
console.log("We are going to be playing music from " + this.artist + "tonight!"); }
let battle = {  artist:"acdc",   playGig: function(){     //instruments that your band will use     let instruments= ["microphone", "electricguitar", "bass", "drumset"];
console.log("We are going to be playing music from " + this.artist + "tonight!");   } }
let practice = battle.playGig; //run a practice practice();

So you are probably wondering: what is the execution context of the last line?

因此,您可能想知道:最后一行的执行上下文是什么?

Well, this will run into a similar problem as the previous example. When we create the practice variable, we are now storing an instance of the playGig method in the global context! It is no longer in the context of the battle object.

好吧,这将遇到与前面的示例类似的问题。 当我们创建练习变量时,我们现在将playGig方法的实例存储在全局上下文中 ! 它不再是在战斗对象的上下文中。

If we ran the code above, we would get:

如果运行上面的代码,则会得到:

practice();
//"We are going to be playing music from Billy Joel tonight!"

Not what we want. We are trying to practice AC/DC, and instead practicing Billy Joel. Yikes.

不是我们想要的。 我们正在尝试练习AC / DC,而是练习Billy Joel。 kes

Instead, we need to use the bind() method just like above. This will allow us to bind the context of the battle object.

相反,我们需要像上面一样使用bind()方法。 这将使我们能够绑定战斗对象的上下文。

let practice = battle.playGig.bind(battle);
practice(); //"We are going to be playing music from AC/DC tonight!"

匿名函数如何影响上下文 (How Anonymous Functions Affect Context)

Let’s say that your gig is coming to a close, and you want to give a shoutout to everyone in your band so that the crowd can give each person a round of applause.

假设您的演出即将结束,并且您想对乐队中的每个人大喊大叫,以便群众可以给每个人鼓掌。

In order to do this, we are going to use the forEach() method to iterate through each element in the value of the instruments property. (You will see why we changed it from a variable to a property in a moment). It will look like this:

为了做到这一点,我们将使用forEach()方法来遍历instruments属性值中的每个元素。 (您将看到为什么我们马上将其从变量更改为属性)。 它看起来像这样:

let battle = {  artist:"acdc",
//instruments that your band will use  instruments: ["microphone", "electricguitar", "bass", "drumset"],
shoutout: function(){
this.instruments.forEach(function(instrument){      console.log("Give a shoutout to my friend for covering the " + instrument + " from " + this.artist + "!");     }   } }
battle.shoutout();

But yet again, if we ran this code, it would not work.

但是同样,如果我们运行这段代码,它将无法正常工作。

It all centers around the line where we declare an anonymous function to use on each element in instruments. When this function is executed, the first this will retain the correct context: the battle object.

所有这些都围绕我们在其中声明要在工具中的每个元素上使用的匿名函数的那一行。 当执行此功能时,首先,这将保留正确的上下文: 战斗对象。

But, when we arrive at this.artist in the console.log statement, we will get… “Billy Joel”. This is because of the anonymous function that is used as a callback in the forEach() method. It resets the scope to the global scope.

但是,当我们在console.log语句中到达this.artist时,我们将得到“ Billy Joel”。 这是因为匿名函数在forEach()方法中用作回调。 它将范围重置为全局范围。

In this case, that means we would claim at the end to be playing Billy Joel… d’oh!

在这种情况下,这意味着我们最终会声称自己正在玩Billy Joel ... d'哦!

But here’s what we can do. We can create a new variable called that to store this in the correct context. Then, when we reference the artist that we played in this specific gig, we can reference the stored context, rather than being forced to return to global context.

但是,这就是我们可以做的。 我们可以创建一个名为存储在正确的背景下新的变量。 然后,当我们参考在该特定演出中扮演的艺术家时,我们可以参考存储的上下文,而不必强迫返回全局上下文。

let battle = {  artist:"acdc",  //instruments that your band will use   instruments: ["microphone", "electricguitar", "bass", "drumset"],   shoutout: function(){
//store context of this     let that = this;
this.instruments.forEach(function(instrument){      console.log("Give a shoutout to my friend for covering the " + instrument + " from " + that.artist + "!");    }   } }
battle.shoutout();

获取最新教程 (Get The Latest Tutorials)

Did you enjoy this tutorial? If you did, give it a clap or sign up for the latest visual tutorials from CodeAnalogies here:

您喜欢本教程吗? 如果您这样做了,请鼓掌或在此处注册CodeAnalogies的最新视觉教程:

翻译自: https://www.freecodecamp.org/news/javascripts-this-explained-by-starting-a-high-school-band-e072c8035eae/

相关文章:

Ubuntu 16.04 安装Django

> pip install django1.10.3......或者:> pip3 install django1.10.3(我采用)......或者:>python3 -m pip install django1.10.3......或者:pip install -i https://pypi.douban.com/simple/ django1.10.3......如果你只安装一个版本的 Python,…

【微信小程序之画布】三:手指触摸绘画椭圆

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文: 功能:根据手指触摸绘画一个椭圆路径--> 起点为手指开始触摸时的坐标,终点为手指触摸结束时的坐标 效果图: 组件属性的描述在上一节中描述过了&…

ACdream 1099——瑶瑶的第K大——————【快排舍半,输入外挂】

瑶瑶的第K大Time Limit:2000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu Submit Status Practice ACdream 1099Description 一天,萌萌的妹子--瑶瑶(tsyao)很无聊,就来找你玩。可是你们都不知道玩什么。。。尴尬了一阵子&#xff…

开源贡献 计算_如何克服恐惧并为开源做贡献

开源贡献 计算Are you a new developer? Or maybe even just an old-timer who has been in a company for ten years, working on an in-house project, and now you’re thinking, “Hey, I’ve been in my box a long time. What’s new out there?” I have been like th…

Android学习笔记进阶十一图片动画播放(AnimationDrawable)

大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它。它的使用更加简单,只需要创建一个 AnimationDrawabledF对象来表示Frame动画,然后通过addFrame 方法把每一帧要显示的内容添加进去,并设置播放间隔时间&#xf…

JS 把url的参数解析成对象

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文: 实现思路:请看log和打印结果 // url参数解析 function getUrlkey(url) {var params {};var urls url.split("?"); console.log(1_分割url:,…

Objective-C代码的文件扩展名

转载于:https://www.cnblogs.com/123qw/p/4375299.html

公司成立两周年感言_对我的副项目成立一周年的一些反思

公司成立两周年感言by Will Abramson威尔艾布拉姆森(Will Abramson) 对我的副项目成立一周年的一些反思 (Some reflections on my side project’s first anniversary) My side project turns one this month. It has been a real learning roller coaster.我的副业这个月变成…

2017.4.18 静态代码分析工具sonarqube+sonar-runner的安装配置及使用

配置成功后的代码分析页面: 可以看到对复杂度、语法使用、重复度等等都做了分析,具体到了每一个方法和每一句代码。 四种使用方式: sonarqube sonar-runnersonarqube mavensonarqube eclipsesonarqube IDE IntelliJ使用方式1 &#xff1a…

c中结构体的4种定义

1、常规的标准方式&#xff1a; 1 #include <stdio.h> 2 3 struct student{ 4 int age; 5 float score; 6 char sex; 7 }; 8 9 int main(int argc, char **argv) 10 { 11 struct student studenta { 12 30, 13 79.5, 14 …

js 时间戳与日期处理集合

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 一&#xff1a;获取当前日期 使用方式&#xff1a;console.log(util.formatTime()) 打印结果&#xff1a;2018/04/24 11:06:45 // 获取当前日期 function formatTime() {var date…

超赞网站推荐_字体(更多)超赞-标志性发明

超赞网站推荐by Pubudu Dodangoda通过Pubudu Dodangoda 字体(更多)超赞-标志性发明 (Font (More) Awesome — an iconic invention) Whether you are building a website, a mobile app, or even a standalone app, there are few things you can never escape. The proper us…

JAVA中的垃圾回收机制以及其在android开发中的作用

http://blog.csdn.net/xieqibao/article/details/6707519 这篇文章概述了JAVA中运行时数据的结构&#xff0c;以及垃圾回收机制的作用。在后半部分&#xff0c;描述了如何检测和定位ANDROID程序是否内存溢出。转载于:https://www.cnblogs.com/u3shadow/p/4379336.html

微信小程序把后台传过来的数组坐标展示在地图上

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 功能实现&#xff1a; 1. 根据后台传递过来的数据&#xff0c;包括地址名字&#xff0c;经纬度坐标等都展示在map组件上&#xff1b; 2. 点击相应地址实现用户当前位置导航至点击的…

Flask-login Question

1 未登录访问鉴权页面如何处理&#xff1f; 如果未登录访问了一个作了 login_required 限制的 view&#xff0c;那么 Flask-Login 会默认 flash一条消息&#xff0c;并且将重定向到login_view&#xff0c;如果你没有指定login_view&#xff0c;那么 Flask-Login 将会抛出一个 4…

愉快的舞会c++_如何在5分钟内建立一个令人愉快的加载屏幕

愉快的舞会cFirst, here is what we will build. Set your timer!首先&#xff0c;这是我们将要建立的。 设置您的计时器&#xff01; Does this look familiar?这看起来很熟悉吗&#xff1f; If yes, that’s because you’ve seen this somewhere — Slack!如果是&#xf…

有关C/C++中,表达式计算顺序的问题,以及表达式内部变量“副作用”问题(转)...

经常可以在一些讨论组里看到下面的提问&#xff1a;“谁知道下面C语句给n赋什么值&#xff1f;”m 1; n mm;最近有位不相识的朋友发email给我&#xff0c;问为什么在某个C系统里&#xff0c;下面表达式打印出两个4&#xff0c;而不是4和5&#xff1a;a 4; cout << a &…

HDU 3001

题目中说明每个城市至少要走一次&#xff0c;至多走2次&#xff0c;因此要用到三进制压缩&#xff0c;然后就是状态转移方程了。 这道题就处理三进制的地方麻烦一点。同时注意&#xff0c;在选择最小长度时&#xff0c;一定是要每一个点都经过至少一次的&#xff0c;即是状态的…

微信小程序 侧滑效果实现

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 先看效果图&#xff1a; 源码&#xff1a; <view wx:if{{if_show}} class{{show_centent?"show":"hide"}} /> <button bindtapbtn>展示 or 隐藏&l…

im和音视频开发哪个更好_如何阅读成为更好的开发者的方式

im和音视频开发哪个更好by nolan grace通过诺兰格雷斯 如何阅读成为更好的开发者的方式 (How to read your way to becoming a better developer) If you want to get better at programming, there are two things you need to do:如果您想提高编程水平&#xff0c;则需要做两…

MVC3学习 四 EF删除操作

由于EF的框架是4.1的&#xff0c;所以现在如果想更新部分字段的话&#xff0c;只能从数据库中查出一次数据&#xff08;不用查的方法还没找到&#xff0c;需要继续研究&#xff09;&#xff0c;不能像5.1的版本可以不用查。 更新的Action需要用到[HttpGet]和[HttpPost]&#xf…

ThinkPHP5.0中Redis的使用和封装(原创)

Redis是一种常用的非关系型数据库,主要用作数据缓存,数据保存形式为key-value,键值相互映射.它的数据存储跟MySQL不同,它数据存储在内存之中,所以数据读取相对而言很快,用来做高并发非常不错. ThinkPhP5.0自带了Redis扩展,在使用之前先下载php_redis.dll 网址 http://windows.p…

【微信小程序之画布】四:手指触摸绘波浪线

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 功能&#xff1a;根据手指触摸绘画一条直线路径--> 起点为手指开始触摸时的坐标&#xff0c;终点为手指触摸结束时的坐标 效果图&#xff1a; 上代码&#xff1a; <canvas clas…

saas的计费数据库设计_如何构建和扩展SaaS计费解决方案

saas的计费数据库设计您需要的最低可行产品 (What you need for a Minimum Viable Product) When you are building your Software as a Service (Saas) Minimum Viable Product (MVP), there is a lot of work that needs to be done. It can be difficult to balance this wo…

关于一对多,多对多的多表查询的控制

一、一对多 以班级Classes和学生Student为例&#xff1a;回忆sql语句://内链接,两种方式效果一样,查询的是两边都有的数据SELECT c.*,s.* FROM classes c,student s WHERE s.cidc.cid;SELECT c.cname,s.sname FROM classes c INNER JOIN student s ON s.cidc.cid;//左外连接&am…

JavaScript对象,方括号和算法

by Dmitri Grabov德米特里格拉波夫(Dmitri Grabov) JavaScript对象&#xff0c;方括号和算法 (JavaScript Objects, Square Brackets and Algorithms) One of the most powerful aspects of JavaScript is being able to dynamically refer to properties of objects. In this…

《Java 8 实战》(二)—— Lambda

Lambda表达式可以理解为简洁地表示可传递的匿名函数的一种方式&#xff1a;它没有名称&#xff0c;但它有参数列表/函数主体/返回类型&#xff0c;可能还有一个可以抛出的异常列表。 Lambda表达式由参数/箭头和主体组成&#xff1a; (Apple a1, Apple a2) -> a1.getWeight(…

c++回调函数 callback

&#xff08;1&#xff09;Callback方式Callback的本质是设置一个函数指针进去&#xff0c;然后在需要需要触发某个事件时调用该方法, 比如Windows的窗口消息处理函数就是这种类型。比如下面的示例代码&#xff0c;我们在Download完成时需要触发一个通知外面的事件&#xff1a;…

【微信小程序之画布】终:手指触摸画板实现

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 先看效果图&#xff1a; wxml <!--pages/shouxieban/shouxieban.wxml--> <view class"container"><view>手写板&#xff08;请在下方区域手写内容&…

Android开发中应避免的重大错误

by Varun Barad由Varun Barad Android开发中应避免的重大错误 (Critical mistakes to avoid in Android development) As many pioneers and leaders in different fields have paraphrased:正如许多不同领域的开拓者和领导人所说&#xff1a; In any endeavor, it is import…