Javascript OOP中嵌套函数的调用
我正试图更加熟悉Javascript OOP,因此我编写了一个小测试脚本,但在测试时,我总是遇到一个异常: 例外情况:Javascript OOP中嵌套函数的调用,javascript,oop,Javascript,Oop,我正试图更加熟悉Javascript OOP,因此我编写了一个小测试脚本,但在测试时,我总是遇到一个异常: 例外情况: Uncaught TypeError: Cannot call method 'day' of undefined (function () { function Time (date) { var self = this; var timeInWeek = 604800000; var timeInDay = 8640
Uncaught TypeError: Cannot call method 'day' of undefined
(function () {
function Time (date) {
var self = this;
var timeInWeek = 604800000;
var timeInDay = 86400000;
var dateInMilliSeconds = date.getTime();
self.add = function (num) {
self.day = function () {
var newDate = new Date();
newDate.setTime(dateInMilliSeconds + (timeInDay * num));
return newDate;
};
};
};
var date = new Date();
var time = new Time(date).add(1).day();
console.log(time);
})();
代码:
Uncaught TypeError: Cannot call method 'day' of undefined
(function () {
function Time (date) {
var self = this;
var timeInWeek = 604800000;
var timeInDay = 86400000;
var dateInMilliSeconds = date.getTime();
self.add = function (num) {
self.day = function () {
var newDate = new Date();
newDate.setTime(dateInMilliSeconds + (timeInDay * num));
return newDate;
};
};
};
var date = new Date();
var time = new Time(date).add(1).day();
console.log(time);
})();
当我在IIFE模式之外运行测试脚本时,我得到了一个异常
Time is undefined
,我是Javascript OOP新手,所以当我尝试阅读其他Javascript库时,一块很好的代码让我不知所措。非常感谢您的帮助。由于没有return
语句,add
方法返回未定义的。这个问题与生命的使用无关
相反,add
方法只是在执行时添加(可能会覆盖)了day
方法,因此以下方法会“起作用”:
然而,我怀疑问题有两个方面:
添加
应返回
的兼容类型的对象;这可以是相同的对象(对于可变设计),也可以是新对象(对于不可变设计)day
应直接添加到每个时间对象中;这将使新的时间(date.day()
起作用function Time (date) {
var self = this;
var timeInWeek = 604800000;
var timeInDay = 86400000;
var dateInMilliSeconds = date.getTime();
self.add = function (num) {
// The idea here is to return an object of the same type for "chaining".
// Here I returned a new Time object (you'll have to work out the details),
// although for mutable objects, "return self" would be appropriate.
return new Time(dateInMilliSeconds + (timeInDay * num));
};
self.day = function () {
// actually return the "day", whatever that is.
return ...;
};
};
虽然这可能是一个很好的练习,但对于生产代码,我建议您使用,除非另有令人信服的理由。moment.js的源代码(这可能是一个很好的参考)已打开。“添加”(包括遗漏和附加注释)如下所示:
add : function (input, val) {
// mutate this objects data (but not methods)
addOrSubtractDurationFromMoment(this, dur, 1);
// returns the same object for chaining
return this;
},
add
方法返回undefined,因为它没有return
语句。这个问题与生命的使用无关
相反,add
方法只是在执行时添加(可能会覆盖)了day
方法,因此以下方法会“起作用”:
然而,我怀疑问题有两个方面:
添加
应返回
的兼容类型的对象;这可以是相同的对象(对于可变设计),也可以是新对象(对于不可变设计)day
应直接添加到每个时间对象中;这将使新的时间(date.day()
起作用function Time (date) {
var self = this;
var timeInWeek = 604800000;
var timeInDay = 86400000;
var dateInMilliSeconds = date.getTime();
self.add = function (num) {
// The idea here is to return an object of the same type for "chaining".
// Here I returned a new Time object (you'll have to work out the details),
// although for mutable objects, "return self" would be appropriate.
return new Time(dateInMilliSeconds + (timeInDay * num));
};
self.day = function () {
// actually return the "day", whatever that is.
return ...;
};
};
虽然这可能是一个很好的练习,但对于生产代码,我建议您使用,除非另有令人信服的理由。moment.js的源代码(这可能是一个很好的参考)已打开。“添加”(包括遗漏和附加注释)如下所示:
add : function (input, val) {
// mutate this objects data (but not methods)
addOrSubtractDurationFromMoment(this, dur, 1);
// returns the same object for chaining
return this;
},
您错过了一个
返回此项代码>(必须在那里才能允许菊花链)
如果没有return
语句,则在newtime(date).add(1).day())
中,将day()
视为newtime()
实例的add
方法返回的值的方法。如果add
方法未返回具有day
方法的对象,则肯定会出现错误
返回此
仅用于链接。
您的代码可以与
var time = new Time(date);
time.add(1);
time.day();
或
您错过了一个返回此项代码>(必须在那里才能允许菊花链)
如果没有return
语句,则在newtime(date).add(1).day())
中,将day()
视为newtime()
实例的add
方法返回的值的方法。如果add
方法未返回具有day
方法的对象,则肯定会出现错误
返回此
仅用于链接。
您的代码可以与
var time = new Time(date);
time.add(1);
time.day();
或
谢谢,伙计。抓到的不错。谢谢,伙计。抓到的不错。这是很好的信息,谢谢。我知道moment.js,我只是想要一些js OOP实践,我会仔细看看它的源代码。我使用了两种不同的返回,我注意到返回day
和add
函数中的任何其他函数作为对象也可以工作。不建议这样做吗?i、 e.self.add=function(){return{day:function(){…}代码>当然,它在这里“起作用”(因为返回的对象有一个day
method属性),但它可能不适合给定的任务。如果用户执行新时间(..).day()
,会发生什么情况?或者如果他们做了新时间(..).add(..).add(..)
?前者不起作用,因为时间上没有day
方法(仅在add
的结果上),后者不起作用,除非从add
返回的对象也有add
方法。对于链接,通常最好的结果类型与原始对象等效(但通常相同),这样就允许使用所有相同的方法。这是一个很好的信息,谢谢。我知道moment.js,我只是想要一些js OOP实践,我会仔细看看它的源代码。我使用了两种不同的返回,我注意到返回day
和add
函数中的任何其他函数作为对象也可以工作。不建议这样做吗?i、 e.self.add=function(){return{day:function(){…}代码>当然,它在这里“起作用”(因为返回的对象有一个day
method属性),但它可能不适合给定的任务。如果用户执行新时间(..).day()
,会发生什么情况?或者如果他们做了新时间(..).add(..).add(..)
?前者不起作用,因为时间上没有day
方法(仅在add
的结果上),后者不起作用,除非从add
返回的对象也有add
方法。对于链接,结果的类型最好与原始对象等效(但通常相同),这样就允许使用所有相同的方法。