8 Essential Backbone.js Interview Questions *

全数来源的基本问题,最好的骨干.js developers and engineers can answer. 在我们社区的推动下,我们鼓励专家提交问题并提供反馈.

Hire a Top Backbone.js Developer Now
Toptal logo是顶级自由软件开发人员的专属网络吗, designers, finance experts, product managers, and project managers in the world. 顶级公司雇佣Toptal自由职业者来完成他们最重要的项目.

Interview Questions

1.

What are some alternatives to Backbone.js’s dependencies?

View answer

Backbone.js has only one hard dependency: Underscore.js. However, you often need to include jQuery and json2.js to support certain features. 有时可以使用Lo-Dash和Zepto(两种轻量级的Underscore替代品).js and jQuery) when it comes to using Backbone.js.

2.

为什么要使用“listenTo()”而不是“on()”来绑定事件处理程序??

View answer

使用“listenTo()”而不是使用“on()”有两个优点. Typically, the way they are used is a bit different:

listener.listenTo(object, event, callback)
object.on(event, callback)

使用" listenTo() ",您想要侦听其事件的对象将作为第一个参数传递. 在“on()”的情况下,它实际上是该对象的一个方法.

The advantages of “listenTo()” over “on()” are:

  • The listener keeps track of all the event handlers, 这样在需要的时候可以更容易地一次性移除它们.
  • 回调的上下文总是设置为侦听器本身.
3.

How do you sort a collection? 在集合上显式调用“sort()”什么时候很重要?

View answer

With Backbone.在Js中,可以通过在集合对象上定义比较器对集合进行排序. By default, collections are not explicitly sorted. By defining a comparator, 每当添加模型或在集合上调用" sort() "方法时,都会对集合进行排序:

var Fruits = Backbone.Collection.extend({
	comparator: function(a, b) { /* .. */ }
})
// Or
var Fruits = Backbone.Collection.extend({})
var fruits = new Fruits()
fruits.comparator = function(a, b) { /* .. */ }

comparator属性可以是具有一个参数(类似于“sortBy”中使用的参数)或两个参数(类似于“sort”中使用的参数)的函数。, 或按名称标识要排序的属性的字符串.

当集合中模型的属性发生变化时,集合不会对自己进行排序. 在这种情况下,必须显式调用sort来更新集合中模型的顺序.

Apply to Join Toptal's Development Network

and enjoy reliable, steady, remote Freelance Backbone.js Developer Jobs

Apply as a Freelancer
4.

What function would you change to override Backbone.js’s default support for REST APIs?

View answer

为了覆盖每个模型的默认行为,您可以设置一个自定义函数为“Model”.sync”. 要使更改全局,可以将自定义函数设置为“Backbone”.sync”. 理想情况下,“sync”函数应该处理四个方法:“创建”、“读取”、“更新”和“删除”。. The function receives the CRUD method name, the model itself, and an object with some additional options. Sometimes setting “Backbone.emulateJSON” to true can often do the job, 在这种情况下,你所需要做的就是提交请求为" application/x-www-form-urlencoded "而不是" application/json ".

5.

如何监视模型的单个属性的更改?

View answer

每当模型中的一些数据发生变化时,模型对象就会触发“change”事件. However, 对象触发另一个事件,其名称特定于已更改的属性:" change:[attribute] ". For example:

var Fruit = Backbone.Model.extend({})
var fruit = new Fruit({
	weight: 3.25
})
fruit.on(‘change:weight, function() {
	//事件" change:weight "将在水果的weight属性发生变化时触发.
})
6.

你如何定义一个模型,当你试图获取或保存使用URL“/api/records/{timestamp}”? 其中“{timestamp}”是自epoch以来经过的秒数.

View answer

模型的url通过设置“url”属性来定义. The attribute can be set as a string or a function. 我们可以设置一个函数来返回一个满足URL模式的字符串:

var Record = Backbone.Model.extend({
	url: function() {
		return ‘/api/records/’+Math.round(new Date().getTime()/1000)
	}
})

另一种不太推荐的方法是无论何时何地调用“fetch()”都使用适当生成的URL。.

7.

为什么不建议直接更改视图的“el”属性? How should it be done instead?

View answer

试图改变“el”属性可能会导致Backbone视图中的行为不一致. 这是因为直接更改“el”属性不会自动更新与之对应的缓存jQuery对象属性, which is “$el”. 正确的方法是在视图上使用" setElement() ":

view.setElement(otherElement)
8.

属性“id”和“cid”在模型对象上的区别是什么?

View answer

模型上的“id”属性是根据模型属性散列中设置的“id”自动分配的. 理想情况下,这是您从正在查询的资源的其他API接收到的ID. On the other hand, “cid”是临时分配给每个模型的ID,在确定对象的实际ID之前是有用的. For example, 推送到尚未持久化的集合的模型可以使用“cid”来处理。, 直到它被保存在数据库中,并为它生成一个实际的ID.

面试不仅仅是棘手的技术问题, so these are intended merely as a guide. 并不是每一个值得雇佣的“A”候选人都能回答所有的问题, nor does answering them all guarantee an “A” candidate. At the end of the day, hiring remains an art, a science — and a lot of work.

Why Toptal

Tired of interviewing candidates? Not sure what to ask to get you a top hire?

Let Toptal find the best people for you.

Hire a Top Backbone.js Developer Now

Our Exclusive Network of Backbone.js Developers

Looking to land a job as a Backbone.js Developer?

Let Toptal find the right job for you.

Apply as a Backbone.js Developer

Job Opportunities From Our Network

Submit an interview question

提交的问题和答案将被审查和编辑, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.

* All fields are required

Looking for Backbone.js Developers?

Looking for Backbone.js Developers? Check out Toptal’s Backbone.js developers.

Alejandro Hernandez

Freelance Backbone.js Developer
ArgentinaToptal Member Since October 30, 2012

Alejandro于2005年获得软件工程学士学位,此后一直作为自由职业者为全球各种规模的软件公司工作. Currently, 他喜欢在JavaScript项目中担任全栈架构师, 他的经验和他对建筑和理论的深刻理解最具影响力的是什么.

Show More

Federico Dayan

Freelance Backbone.js Developer
United StatesToptal Member Since September 16, 2013

Federico是一位充满激情的高级全栈开发人员,拥有超过十年的网络生态系统工作经验. 他在帮助客户发展想法并将其变为现实方面拥有丰富的经验. Federico认为,干净的代码和关注点分离造就了优秀的软件.

Show More

Reuf Slamnik

Freelance Backbone.js Developer
Bosnia and HerzegovinaToptal Member Since November 19, 2012

Reuf is a results-oriented, 以客户为中心的高级软件工程师,有14年以上的分析经验, developing, and troubleshooting software and system architectures. 他有很强的设计和解决问题的能力,特别是在React、Angular和 .NET.

Show More

Toptal Connects the Top 3% of Freelance Talent All Over The World.

Join the Toptal community.

Learn more