我注意到,似乎没有明确解释this
关键字是什么以及如何在Stack Overflow站点上的JavaScript中正确(和错误地)使用它.
我亲眼目睹了一些非常奇怪的行为,并且无法理解为什么会发生这种行为.
this
工作如何以及何时使用?
我在WebKit HTML 5 SQL存储笔记演示的源代码中看到以下内容:
function Note() {
var self = this;
var note = document.createElement('div');
note.className = 'note';
note.addEventListener('mousedown', function(e) { return self.onMouseDown(e) }, false);
note.addEventListener('click', function() { return self.onNoteClick() }, false);
this.note = note;
// ...
}
Run Code Online (Sandbox Code Playgroud)
笔者采用自我在一些地方(函数体)及本在其他地方(的函数方法的参数列表中定义的机构).这是怎么回事?现在我已经注意到了它,我会在各处开始看到它吗?
以下是否正确地将Disqus嵌入到Angular2组件中
disqus.component.html
<div class="card card-block">
<div id="disqus_thread"></div>
<noscript>
Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript" rel="nofollow">comments powered by Disqus.</a>
</noscript>
</div>
Run Code Online (Sandbox Code Playgroud)
-
disqus.component.ts
//Above code omitted for simplicity
export class DisqusComponent {
ngOnInit(){
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//blahblahblahdafsfawf.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
}
}
Run Code Online (Sandbox Code Playgroud)