Javascript函数没有被添加到对象原型中
Javascript functions not being added to the object prototype
我有几个在我的代码中向javascript类添加原型的其他实例,这些实例工作得很好,我无法确切地弄清楚为什么我刚刚写的部分拒绝工作。
问题的部分是:
var opt = new Settings;
function Settings() {
this.initjQuerySelectors();
this.sorted_columns_url = this.getSortedColumnsUrl();
}
Settings.prototype.initjQuerySelectors = function() {
this.data_store_selector = $('.data_store.display');
};
Settings.prototype.getSortedColumnsUrl = function() {
return this.data_store_selector.val();
};
,并产生以下错误:
Uncaught TypeError: Object #<Settings> has no method 'initjQuerySelectors'
当我试图捕获错误时,它指出也没有getSortedColumnsUrl();
的方法。
用chrome开发工具查看代码,它显示我添加的设置原型中确实没有任何内容,问题是我无法找出原因。
这可能是因为您的代码顺序不正确,
尝试先定义原型,然后再实例化opt对象。
Settings.prototype.initjQuerySelectors = function() {
this.data_store_selector = $('.data_store.display');
};
Settings.prototype.getSortedColumnsUrl = function() {
return this.data_store_selector.val();
};
function Settings() {
this.initjQuerySelectors();
this.sorted_columns_url = this.getSortedColumnsUrl();
}
var opt = new Settings;
编辑:在扩展它的原型以完全跨浏览器支持之前定义函数。(感谢@Turnerj修复)
相关文章:
- 重载JS'firefox中的对象原型
- 带有对象/原型的链式承诺(Q延期)
- 别名或以其他方式合并两个具有不同名称的相同对象原型
- 创建后指定对象原型
- javascript对象原型与jquery冲突
- 为什么我要将函数添加到对象原型中,而不是将其添加到对象中
- 基本对象/原型语法问题
- Javascript对象/原型.我的理解错了吗
- ES6 const,用于在JavaScript中创建对象原型;这是一种模式吗
- 在角度表达式中使用对象原型
- 对象原型和继承
- 为什么javascript对象原型被称为“fn”
- Uncaught TypeError:对象原型在Symfony 2中只能是Object或ExtJS为null
- 在Javascript中将对象原型函数绑定到对象
- 对象原型在Canvas Game中不起作用
- 如何获取Javascript对象原型的属性
- 为对象原型提供一个方法,该方法指向对象自身属性的值
- 向对象原型添加命名空间方法
- 在Vanilla Javascript中使用窗体中的对象原型创建多个用户
- Javascript对象原型错误