惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
K
Kaspersky official blog
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cisco Blogs
AWS News Blog
AWS News Blog
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
T
Threatpost
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
Blog — PlanetScale
Blog — PlanetScale
C
Cybersecurity and Infrastructure Security Agency CISA
F
Full Disclosure
博客园_首页
N
Netflix TechBlog - Medium
Security Latest
Security Latest
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Recent Announcements
Recent Announcements
博客园 - Franky
P
Palo Alto Networks Blog
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 【当耐特】
GbyAI
GbyAI

Backbone.js

渣渣想请教下有关backbone的一点入门理解问题 关于BackboneJS中extend实现的理解 - V2EX Backbone.js的兼容性怎么样?ie6能支持吗 - V2EX 有用过Backbone的吗,backbone在不同路由跳转的时候可以从服务器取模板吗?还是说要所有的模板都要取回来? js模板过于集中的问题 请问国内有什么关于Backbone.js的社区么? - V2EX
backbone.js的例子问题 - V2EX
ljbha007 · 2012-12-28 · via Backbone.js

这是一个创建于 4916 天前的主题,其中的信息可能已经有所发展或是发生改变。

最近在学习backbone.js
看到第一个例子就有看不懂的地方
http://arturadib.com/hello-backbonejs/docs/1.html

(function($){
var ListView = Backbone.View.extend({
...
initialize: function(){
_.bindAll(this, 'render'); // fixes loss of context for 'this' within methods

this.render(); // not all views are self-rendering. This one is.
},
...
});
...
})(jQuery);

请问:
Q1: 为什么用 (function($){})(jQuery);而不用 (function(){})();?

Q2: _.bindAll(this, 'render') 是干啥的? 为什么可以“fixes loss of context for 'this' within methods”?

麻烦懂的解答一下 感激不尽

zythum

1

zythum      2012 年 12 月 28 日   ❤️ 1

Q1.
比如
(function($){
setTimeout(function(){
alert($);
},200)
})(JQuery);
JQuery = null;

(function(){
setTimeout(function(){
alert(jQuery);
},200)
})();
jQuery = null;

这样就应该能理解了。

2. 就是把这些自定义事件绑到this上

ljbha007

2

ljbha007      2012 年 12 月 28 日

@zythum
谢谢
第一个我明白了
第二个“把这些自定义事件绑到this上”是什么意思?这样的绑定又是怎么防止"loss of context for 'this' within methods"的呢?

zythum

3

zythum      2012 年 12 月 28 日

Q2. 其实解释改下this的上下文。
比如
var a = {
click: function(){alert(this)}
}
a.click(); => alert window

_.bindAll(a,'click');
DOM.bind('click', a.click)
点击 => alert DOM

zythum

4

zythum      2012 年 12 月 28 日

@ljbha007 我 backbone不怎么用。 应该是这样。如果不对请指正

FuryBean

5

FuryBean      2012 年 12 月 28 日

Q1: 提供使用其他选择器的可能,比如zepto.js

Q2:_.bindAll的文档看这里:http://underscorejs.org/#bindAll
如果了解bind,看_.bind的说明:
Bind a function to an object, meaning that whenever the function is called, the value of this will be the object. Optionally, bind arguments to the function to pre-fill them, also known as partial application.

ljbha007

6

ljbha007      2012 年 12 月 28 日

@zythum
第一段代码运行结果不是window是a本身[Object object]
第二个运行不了

zythum

7

zythum      2012 年 12 月 28 日

@ljbha007
恩。第一个应该是a 我错了。
DOM是一个dom么。比如 $(document.body)之类的。

ljbha007

8

ljbha007      2012 年 12 月 28 日

@zythum
不过我最后搞懂那个_.bindAll的意思和原理了 貌似是利用.apply或者.call方法来注入一个this对象

jinwyp

10

jinwyp      2012 年 12 月 28 日

_.bindall 现在不用了都用 model.on 了 网站的教程比较老 看这个吧