1

なぜこれが機能しないのかを理解しようとしています。(まだ検証されていない基本的な例)

テストすると、firebug は Product.addPage が見つからないと述べています。

var Product = function ()
{
    var Page = function ()
    {
        var IMAGE = '';

        return {
            image : function ()
            {
                return IMAGE;
            },
            setImage : function (imageSrc_)
            {
                IMAGE = '<img id="image" src="' + imageSrc_ + '" height="100%" width="100%">';
            }
        };
    };
    var PAGES = [];

    return {
        addPage : function ()
        {
            var len = PAGES.length + 1;
            PAGES[len] = new Page();
            return PAGES[len];
        },
        page : function (pageNumber_)
        {
            var result = PAGES[pageNumber_];
            return result;
        }
    };
};

// Begin executing
$(document).ready(function ()
{
    Product.addPage.setImage('http://site/images/small_logo.png');
    alert(Product.page(1).image());
});
4

4 に答える 4

8

返されたオブジェクトではなく、 Product関数(この場合はコンストラクター)の addPage プロパティを参照しようとしています。

おそらく次のようなものが必要です。

// Begin executing
$(document).ready(function ()
{
    var product = new Product();
    product.addPage().setImage('http://site/images/small_logo.png');
    alert(product.page(1).image());
});
これにより、addPage 呼び出しに括弧が追加されます (ただし、FireBug がそのメソッドを見つけることができないため、これは問題ではありません)。

于 2009-01-15T16:57:12.807 に答える
2

どうですか?Product.addPage().setImage('http://site/images/small_logo.png');


編集:問題の半分しかキャッチできなかったことがわかりました。全体に対するdtsazzaの答えを見てください。

于 2009-01-15T16:56:42.293 に答える
0

どうですか:

var Product = {
    Page : function() {             
        return {
            _image : '',
            Image : function() {
                return this._image;
            },
            setImage : function(imageSrc_) {
                this._image = '<img id="image" src="' + imageSrc_ + '" height="100%" width="100%">';
            }
        };
    },
    Pages : [],
    addPage : function() {
        var Page = new Product.Page();
        this.Pages.push(Page);
        return Page;
    },
    GetPage : function(pageNumber_) {
        return this.Pages[pageNumber_];
    }
};

// Begin executing
$(document).ready(function ()
{
    Product.addPage().setImage('http://site/images/small_logo.png');
    alert(Product.GetPage(0).Image());
});
于 2009-01-16T15:37:10.857 に答える
0

これもうまくいきます:

Product().addPage().setImage('http://site/images/small_logo.png');
于 2009-01-15T17:21:13.307 に答える