es6 promise

 

 
for (var i = 0; i < 5; i++) {
  console.log(i);
}

“小伟,你说说这几行代码会输出什么?”

当面试官在 Sublime 打出这几行代码时,我竟有点蒙蔽。蛤?这不是最简单的一个循环吗?是不是有陷阱啊,我思索一下,这好像和我看的那个闭包的题很像啊,这面试官是不是没写完啊?有毒啊。

“应该是直接输出 0 到 4 吧...”,我弱弱的说到。

 

for (var i = 0; i < 5; i++) {
  setTimeout(function() {
    console.log(i);
  }, 1000 * i);
}

额,什么鬼,怎么还不是我背了那么多遍的那道闭包题,让我想想。 setTimeout 会延迟执行,那么执行到 console.log 的时候,其实 i 已经变成 5 了,对,就是这样,这么简单怎么可能难到老子。

“应该是开始输出一个 5,然后每隔一秒再输出一个 5,一共 5 个 5。”

“对,那应该怎么改才能输出 0 到 4 呢?”

终于到我熟悉的了,加个闭包就解决了,稳!

for (var i = 0; i < 5; i++) {
  (function(i) {
    setTimeout(function() {
      console.log(i);
    }, i * 1000);
  })(i);
}

“很好,那你能说一下,我删掉这个 i 会发生什么吗?”

for (var i = 0; i < 5; i++) {
  (function() {
    setTimeout(function() {
      console.log(i);
    }, i * 1000);
  })(i);
}

“这样子的话,内部其实没有对 i 保持引用,其实会变成输出 5。”

“很好,那我给你改一下,你看看会输出什么?”

for (var i = 0; i < 5; i++) {
  setTimeout((function(i) {
    console.log(i);
  })(i), i * 1000);
}

蛤?什么鬼,这是什么情况,让我想想。这里给 setTimeout 传递了一个立即执行函数。额,setTimeout 可以接受函数或者字符串作为参数,那么这里立即执行函数是个啥呢,应该是个 undefined ,也就是说等价于:

setTimeout(undefined, ...);

而立即执行函数会立即执行,那么应该是立马输出的。

“应该是立马输出 0 到 4 吧。”

“哎哟,不错哦,最后一题,你对 Promise 了解吧?”

“还可以吧...”

“OK,那你试试这道题。”

setTimeout(function() {
  console.log(1)
}, 0);
new Promise(function executor(resolve) {
  console.log(2);
  for( var i=0 ; i<10000 ; i++ ) {
    i == 9999 && resolve();
  }
  console.log(3);
}).then(function() {
  console.log(4);
});
console.log(5);

 

这道题应该考察我 JavaScript 的运行机制的,让我理一下思路。

首先先碰到一个 setTimeout,于是会先设置一个定时,在定时结束后将传递这个函数放到任务队列里面,因此开始肯定不会输出 1 。

然后是一个 Promise,里面的函数是直接执行的,因此应该直接输出 2 3 。

然后,Promise 的 then 应当会放到当前 tick 的最后,但是还是在当前 tick 中。

因此,应当先输出 5,然后再输出 4 。

最后在到下一个 tick,就是 1 。

“2 3 5 4 1”

 

 

### Link Training vs Relationship Training In the context of scene graph generation, link training and relationship training refer to different aspects of learning how objects interact within an image. #### Link Training Link training focuses on establishing connections between detected objects. This involves identifying pairs of objects that have spatial or semantic relationships. For instance, after obtaining bounding boxes \( B = \{b_i | i = 1, ..., n\} \) and tentative object labels \( L = \{l_i\} \), one can use RoIAlign features \( R = \{r_i\} \) extracted by a pre-trained Faster R-CNN model[^3]. The goal here is to determine which pairs of objects should be connected based on their visual contexts. For example, in MOTIFS, bidirectional LSTMs are used to encode each object's visual context, while VCTree employs bidirectional TreeLSTMs for more complex hierarchical structures. These methods help capture richer contextual information about potential links between objects. #### Relationship Training Relationship training goes beyond just linking objects; it aims at classifying specific types of interactions or predicates describing the nature of these links. Given two linked entities, this process determines what kind of relation exists between them—such as "on," "next to," or "holding." Unbiased approaches like those described aim to mitigate biases present during training so that models generalize better across diverse scenes without overfitting to common but potentially misleading patterns found in biased datasets[^2]. Both forms of training play crucial roles in generating accurate and meaningful scene graphs from images. While link training establishes the structure of relations among identified elements, relationship training enriches this structure with detailed descriptions of inter-object dynamics. ```python def extract_links_and_relationships(objects, roialign_features): """ Extract both links and relationships between objects. Args: objects (list): List of detected objects including bbox and labels. roialign_features (tensor): Features obtained via RoIAlign operation. Returns: tuple: Pairs of linked objects along with predicted relationship types. """ # Placeholder implementation details would go here... pass ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值