Javascript display();没有显示流行,推,不移位,转移?

时间:2015-11-27 03:14:59

标签: javascript push shift pop

我是JavaScript的新手,我正在学习pop,push,shift和unshift。我想要有3部电影,在不同时间显示所有4部电影。但是,我的.display();功能没有显示任何内容。我收到了错误

Uncaught TypeError: ArrayOfMovies.push is not a function

我相信它会与另一个.display();元件。

function Movie(title, genre, rating, price, img) {
    this.title = title;
    this.genre = genre;
    this.rating = rating;
    this.price = price;
    this.image = img;
    this.display = displaymovie;
}

function displaymovie() {
document.write("<blockquote style='text-align:left; font-size:15px; font-family: Arial, sans-sherif; font-weight:600; float:left; padding-left: 20px; padding-right:40px; padding-top:40px; color:#C58917;'>Title:" + this.title + "<br>");
document.write("Genre:" + this.genre + "<br>");
document.write("Rating:" + this.rating + "<br>");
document.write("Price:" + this.price + "</blockquote><br>");
document.write("<span><IMG float:'right' SRC='" + this.image + "' style='width:138px; height:158px;'>" + "<BR><BR>");
}

var movieone = new Movie(" Avengers the age of Ultron", " Action / Adventure", " 8.0/10.0", " $9.99", "Labs_Images/Avg_Ultron.jpg");
var movietwo = new Movie(" The Age of Adaline", " Drama / Romance", " 6.7/10.0", " $8.99, ", "Labs_Images/Tmrr_land.jpg");
var moviethree = new Movie(" The Age of Adaline", " Drama / Romance", " 6.7/10.0", " $8.99", "Labs_Images/MM_road.jpg");

var ArrayOfMovies = new Movie ();
ArrayOfMovies.push(movieone);
ArrayOfMovies.push(movietwo);
ArrayOfMovies.push(moviethree);
var MoviesAll = Movie.push();
     MoviesAll.display();

        document.write("<br> <br>", "<h2>Next</h2>", "<br> <br>");

        var MovieAll = ArrayOfMovies.pop();
         MovieAll.display();
        document.write("<br> <br>", "<h2>Next2</h2>", "<br> <br>");

        var MovieAll2 = ArrayOfMovies.pop();
         MovieAll2.display();
        document.write("<br> <br>", "<h2>Next3</h2>", "<br> <br>");

        var MovieAll3 = ArrayOfMovies.unshift('movietwo');
        var MovieAll4 = ArrayOfMovies.pop();
         MovieAll4.display();
        document.write("<br> <br>", "<h2>Next4</h2>", "<br> <br>");

        var MovieAll5 = ArrayOfMovies.push('moviethree');
        var MovieAll6 = ArrayOfMovies.shift();
         MovieAll6.display();

3 个答案:

答案 0 :(得分:0)

我认为这样的事情就是你正在寻找的:

function Movie(title, genre, rating, price, img) {
    this.title = title;
    this.genre = genre;
    this.rating = rating;
    this.price = price;
    this.image = img;
}

// Just a better way to add methods to the objects prototype
Movie.prototype.display = function() {
    document.write("<blockquote style='text-align:left; font-size:15px; font-family: Arial, sans-sherif; font-weight:600; float:left; padding-left: 20px; padding-right:40px; padding-top:40px; color:#C58917;'>Title:" + this.title + "<br>");
    document.write("Genre:" + this.genre + "<br>");
    document.write("Rating:" + this.rating + "<br>");
    document.write("Price:" + this.price + "</blockquote><br>");
    document.write("<span><IMG float:'right' SRC='" + this.image + "' style='width:138px; height:158px;'>" + "<BR><BR>");
};

var movieone = new Movie(" Avengers the age of Ultron", " Action / Adventure", " 8.0/10.0", " $9.99", "Labs_Images/Avg_Ultron.jpg");
var movietwo = new Movie(" The Age of Adaline", " Drama / Romance", " 6.7/10.0", " $8.99, ", "Labs_Images/Tmrr_land.jpg");
var moviethree = new Movie(" The Age of Adaline", " Drama / Romance", " 6.7/10.0", " $8.99", "Labs_Images/MM_road.jpg");

// Creates an array for the movies
var ArrayOfMovies = [];

// Pushes some movies
ArrayOfMovies.push(movieone);
ArrayOfMovies.push(movietwo);
ArrayOfMovies.push(moviethree);

document.write("<br> <br>", "<h2>Next</h2>", "<br> <br>");

// Proper pop()
var movie = ArrayOfMovies.pop();
movie.display();
document.write("<br> <br>", "<h2>Next2</h2>", "<br> <br>");

// Proper shift()
movie = ArrayOfMovies.shift();
movie.display();
document.write("<br> <br>", "<h2>Next3</h2>", "<br> <br>");

// Proper push() and pop()
ArrayOfMovies.push(movietwo);
movie = ArrayOfMovies.pop();
movie.display();
document.write("<br> <br>", "<h2>Next4</h2>", "<br> <br>");

// Proper unshift() and shift()
ArrayOfMovies.unshift(moviethree);
movie = ArrayOfMovies.shift();
movie.display();

答案 1 :(得分:0)

@torazaburo你必须有分号,因为它是JavaScript编程语言的一部分。如果你没有JavaScript会自动插入分号。查看此博客: - JavaScript Semicolon Insertion: Every you need to know。 它会告诉您有关规则的更多信息。在某些情况下,也可以省略分号。

答案 2 :(得分:0)

在我阅读Sam评论之后,我想出了它需要的更简单的方法:

x[-1]

以下是我的更正后的完整Javascript ---&gt;

var movie = ArrayOfMovies[ArrayOfMovies.length - 3];
movie.display();
var movie = ArrayOfMovies[ArrayOfMovies.length - 2];
movie.display();
var movie = ArrayOfMovies[ArrayOfMovies.length - 1];
movie.display(); //now the pushed are all dispayled!