_createElement 方法的目的是什么?

What is the purpose of the _createElement method?

我正在学习Backbone.js(ver 1.3.3)。

Backbone.View (line 1441)

中有如下一行
_createElement: function(tagName) {
  return document.createElement(tagName);
},

为什么 Backbone.View_createElement(tagName) 方法而不是直接使用 'document.createElement(tagName)'。

当您开发一个将在不同项目中使用的库时,您甚至无法想象,您必须封装所有内容并提供简单的方法来覆盖它的每个细节。这就是 Backbone 的开发者所做的。

现在想象一个使用 Backbone 的程序员不想使用 document.createElement,而是想在他自己的结构中创建节点。然后,他的 BaseNodeView 可以覆盖 _createElement 来处理:

var BaseNodeView = Backbone.View.extend({
    _createElement: function(tagName) {
        return NodeFactory.create(tagName);
    },
});

这正是它的用途,它写在 comments just above

Produces a DOM element to be assigned to your view. Exposed for subclasses using an alternative DOM manipulation API.

该功能是used within the view code抽象背后真正发生的事情,由您决定是否需要默认行为。

某些 Backbone 的 API 没有记录,需要深入研究 the source 以找到可以连接的所有方法。