简体   繁体   English

未从对象内部调用函数

[英]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.

相关问题 在另一个函数中调用时从函数返回的未定义对象 - Undefined object returned from function when called within another function 从另一个对象中调用时,这是javascript函数 - this of a javascript function when called from within another object 我可以声明一个可以在任何对象内调用的函数吗? - Can i declare a function that could be called from within any object? 将函数/属性注入另一个函数/对象,可以从该 function 中的函数调用或读取/设置? - Inject a function/property to another function/object that can be called or read/set from functions within that function? 从对象内部调用的函数是否可以访问该对象的作用域? - Is it possible for a function called from within an object to have access to that object's scope? 从对象调用参数的函数 - Function with parameter called from object 在从对象内部调用时,使用&#39;eval&#39;来定义顶层函数时遇到问题 - Trouble using 'eval' to define a toplevel function when called from within an object 调用 object 后自动调用 Object 内的 function? - Auto call function within Object once object is called? Function 在 Object 内调用时不起作用 - Function Doesn't Work When Called Within Object 使用原型从对象中的对象上调用函数 - Calling a function on an object from within that object with prototype
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM