[英]function not called from within object
So I am trying to follow this book called "Head First: HTML5 Programming" and that means I am trying to learn JavaScript. 因此,我正在尝试遵循《 Head First:HTML5编程》这本书,这意味着我正在尝试学习JavaScript。
I am very used to OOP languages like Java and C# and JS is very new to me because of the whole prototyping aspect. 我非常熟悉Java和C#之类的OOP语言,而JS对于整个原型设计方面来说对我来说还是很新的。 The code below got two functions, that used to work when they were global. 下面的代码有两个函数,它们在全局时可以工作。 After I moved them into my object creation code however, it stopped working altogether. 但是,在将它们移入对象创建代码后,它完全停止了工作。
It's probably a syntax error or perhaps logic error. 这可能是语法错误或逻辑错误。 But I can't seem to point it out myself any where as debugging is not something I've done in JavaScript yet. 但是我似乎无法自己指出任何地方,因为调试还不是我在JavaScript中所做的事情。
Help would be greatly appreciated! 帮助将不胜感激!
function makeMovie(title, genre, rating, showtimes) {
var movie = {
this.title: title,
this.genre: genre,
this.rating: rating,
this.showtimes: showtimes
getNextShowing: function() {
var now = new Date().getTime();
for (var i = 0; i < this.showtimes.length; i++) {
var showtime = this.getTimeFromString(this.showtimes[i]);
if ((showtime - now) > 0) {
return "Next showing of " + this.title + " is " + this.showtimes[i];
}
}
return null;
}
getTimeFromString: function(timeString) {
var theTime = new Date();
var time = timeString.match(/(\d+)(?::(\d\d))?\s*(p?)/);
theTime.setHours( parseInt(time[1]) + (time[3] ? 12 : 0) );
theTime.setMinutes( parseInt(time[2]) || 0 );
return theTime.getTime();
}
}
return movie;
}
var movie1array = ["3:00pm", "7:00pm", "11:00pm"];
var movie2array = ["5:00pm", "9:00pm"];
var movie1 = makeMovie("Plan 9 from Outer Space", "cult classic", 2, movie1array);
var movie2 = makeMovie("Forbidden Planet", "classic sci-fi", 5, movie2array);
and here is the HTML page where I try to call the JavaScript: 这是我尝试调用JavaScript的HTML页面:
<!doctype html>
<html>
<head>
<title>The Webville Theater</title>
<script src="Movie.js"></script>
<meta charset="utf-8">
</head>
<body>
<script>
var nextShowing = movie1.getNextShowing();
document.innerHTML = nextShowing;
nextShowing = movie2.getNextShowing();
document.body.innerHTML = nextShowing;
</script>
</body>
</html>
In an object, properties should not be preceded by 'this'. 在对象中,属性不应以“ this”开头。 'this' is used in function declaration. 'this'用于函数声明。 Each property / value pair is separated by a comma. 每个属性/值对用逗号分隔。
Simple exemple : 简单的例子:
var myObj = {
foo: 'bar',
test: 'foobar',
logFoo: function(){
console.log( this.foo)
}
}
myObj.logFoo(); // log : 'bar'
Your code : 您的代码:
function makeMovie(title, genre, rating, showtimes) {
var movie = {
title: title,
genre: genre,
rating: rating,
showtimes: showtimes,
getNextShowing: function() {
var now = new Date().getTime();
for (var i = 0; i < this.showtimes.length; i++) {
var showtime = this.getTimeFromString(this.showtimes[i]);
if ((showtime - now) > 0) {
return "Next showing of " + this.title + " is " + this.showtimes[i];
}
}
return null;
},
getTimeFromString: function(timeString) {
var theTime = new Date();
var time = timeString.match(/(\d+)(?::(\d\d))?\s*(p?)/);
theTime.setHours( parseInt(time[1]) + (time[3] ? 12 : 0) );
theTime.setMinutes( parseInt(time[2]) || 0 );
return theTime.getTime();
}
}
return movie;
}
var movie1array = ["3:00pm", "7:00pm", "11:00pm"];
var movie2array = ["5:00pm", "9:00pm"];
var movie1 = makeMovie("Plan 9 from Outer Space", "cult classic", 2, movie1array);
var movie2 = makeMovie("Forbidden Planet", "classic sci-fi", 5, movie2array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.