似乎 bind()
是 Web Components 规范的一部分,Polymer 出于各种原因对其进行了扩展 with Node.bind()
.
这是 docs on bindProperty()
.这只是 bind()
的内部实现/polyfill 吗?因此,开发人员是否应该使用 bind()
而不是 bindProperty()
?
最佳答案
答案在bind
的源代码中-line 56 (下面的完整片段)。 bind
函数调用内部 bindProperty
函数。 bind
在 bindProperty
之上所做的一切都是确保给定的属性存在。
bind: function(name, observable, oneTime) {
var property = this.propertyForAttribute(name);
if (!property) {
// TODO(sjmiles): this mixin method must use the special form
// of `super` installed by `mixinMethod` in declaration/prototype.js
return this.mixinSuper(arguments);
} else {
// use n-way Polymer binding
var observer = this.bindProperty(property, observable, oneTime);
// NOTE: reflecting binding information is typically required only for
// tooling. It has a performance cost so it's opt-in in Node.bind.
if (Platform.enableBindingsReflection && observer) {
observer.path = observable.path_;
this._recordBinding(property, observer);
}
if (this.reflect[property]) {
this.reflectPropertyToAttribute(property);
}
return observer;
}
因此,您可以使用 bindProperty
,但我不会推荐它,除非您可以确保要绑定(bind)的属性的存在。
关于javascript - PolymerJS 中的 bind() 和 bindProperty() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27882773/