Javascript OOP - 声明对象的方法
Javascript OOP - declaring methods of an object
这是我能想到的最基本的JS对象示例,它说明了我的问题。
问题1.
如何在类中引用函数,以便在其他代码中调用方法?这给了我一个错误。
var name1 = new Name();
name1.render();
问题2.
像这样内联声明函数与使用 var getByID = function() ...
有什么区别?
示例对象:
function Name(user_id, container_id) {
this.userID = user_id;
this.containerID = container_id;
this.firstName = null;
this.lastName = null;
function getByID(userID) {
// An ajax call that takes a userID to get a name.
}
function setName() {
// An ajax call to get the name from db.
name_record = this.getByID(this.userID); ????? this returns an error that getByID is undefined.
this.firstName = name_record.firstName;
this.lastName = name_record.lastName;
}
function render() {
$(this.containerID).val(this.firstName + ' ' + this.lastName);
}
}
你可以像在第二个问题中所做的那样声明一个对象,它是有效的,因为函数也是一个对象。或其他方式,例如:
var Name = {
render: function() {
}
}
Name.render();
或带原型:
function Name() {
}
Name.prototype.render = function() {
}
// or
Name.prototype = {
getByID: function() {
},
setName: function() {
}
}
var n = new Name();
所有这些狙击手都是有效的对象声明。
你的第二个问题可能会回答第一个问题。当你声明一个这样的函数时:
function Name() {
function render() {
}
}
var n = new Name();
这就像render()
是一个私有方法。 如果在函数名称 n.render()
之外调用,您将看到抛出的错误,因为无法识别render
。但是如果你改成这个...
function Name() {
this.render = function() {
}
}
。然后n.render()
将像render()
成为公共方法一样工作。有关public
和private
方法的更多信息,请参阅此处和此处。
现在,将函数声明为"内联"或将其设置为变量之间的区别在于:
function Name() {
}
你可以做:
var n1 = Name();
var n2 = Name();
var nn = Name(); // and so on...
但有:
var n = function Name() {
}
是n()
会起作用,Name()
不会。即使var a = Name()
也会抛出异常。
这是一篇关于这个主题的好文章,值得一读。我希望它能有所帮助。
问题 1:Javascript中的"类"只不过是一个对象。 对象具有可以访问的属性。 这些属性是变量、函数或其他对象。 通过声明您的函数,例如:
function render() {
$(this.containerID).val(this.firstName + ' ' + this.lastName);
}
您正在函数 Name() 的范围内声明一个函数,但该函数不是 Name 的属性。 这只是一种私人方法。 有多种方法可以使其成为 Name() 的一部分,例如:
function Name(user_id, container_id) {
this.userID = user_id;
this.containerID = container_id;
this.firstName = null;
this.lastName = null;
this.render = function() {
console.log('hello world');
}
}
var name1 = new Name();
name1.render();
问题2:没有区别。 它们只是实现相同结果的两种不同语法。 第二种方法(声明 var 并定义函数)会立即为您提供对函数的引用,但这也可以通过第一种方法实现。
回答您的第一个问题:
函数 getByID,setName 和 render 是构造函数的本地函数,不能由类对象调用。您必须使用原型继承。
例如。
function Name(user_id, container_id) {
this.userID = user_id;
this.containerID = container_id;
this.firstName = null;
this.lastName = null;
}
Name.prototype = {
getByID :function(userID) {
// An ajax call that takes a userID to get a name.
}
setName:function() {
// An ajax call to get the name from db.
name_record = this.getByID(this.userID);
this.firstName = name_record.firstName;
this.lastName = name_record.lastName;
}
render:function() {
$(this.containerID).val(this.firstName + ' ' + this.lastName);
}
};
回答你的第二个问题:
在以下情况下 abc();//error 函数 abc(){ }
这个函数是在运行时创建的,所以只能在声明后调用它
然而,这
abc();
var abc = function(){
};
在解析时创建,以便您可以在声明之前调用它。
- 序列化数据属性中对象的最可靠方法
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 如何从对象的原型方法访问JavaScript对象属性
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- 设置嵌套对象属性的更好方法
- JQuery示例不起作用-“;对象没有't支持属性或方法'按钮'&”;
- 对象不支持属性或方法“自动完成”
- call()和apply()实际上是用来欺骗方法处理类似数组的对象的
- 对象文字方法上的Javascript绑定不起作用
- Meteor应用程序无法运行-对象#<编译器>没有方法'主机'
- 为什么我可以在Array属性对象中找到Javascript Array for Each方法
- Underscore.js某些对象的所有方法的总和
- 对象#<XMLHttpRequest>没有方法'完成'
- Es6:能够在设置/更新/删除对象属性时调用自定义方法
- 丢失对象“;这个“;方法中的上下文
- 如何在ajax中调用javascript对象的方法
- 为什么页面方法对象未定义
- 如何使用在另一个文件中定义的JavaScript方法/对象
- 如何告诉JsHint忽略“未解析的函数或方法”?对象上的警告
- 使用jinja/javascript动态创建方法/对象