0

1 日後、問題を 2 行のコードに絞り込むことができました。たぶん、私はこのステートメントを間違って使用しようとしています。

        function scheduleItemView(myId){
            this.update = function(show){
                document.getElementById(this.id+'-title').innerHTML = show.title +": "+ show.startDate;
                document.getElementById(this.id+'-title-overlay').innerHTML = show.title +": "+ show.startDate;
                document.getElementById(this.id+'-description').innerHTML = truncate(show.description,190);
                document.getElementById(this.id+'-time-start').innerHTML = show.startTime;
                document.getElementById(this.id+'-time-end').innerHTML = show.endTime;
            };
            this.id=myId;
            return true;
        }

        function nowNextView(){
            this.now = new scheduleItemView('now');
            this.next = new scheduleItemView('next');
            this.update = function(type,args){
                var myshow=args[0];

    // problem is below. I have to use the global name to access the update method.   
                    myNowNextView.now.update(myshow.now);
                    myNowNextView.next.update(myshow.next);

    // whereas what I want to do is reference them using the "this" command like below.
    //  this.now.update(myshow.now);
    //  this.next.update(myshow.next);
    // the above doesnt work. The update method in scheduleItemView is not seen unless referenced globally
    // BUT even more infuriating, this.now.id does return "now" so it can access the object, just not the method
    // any ideas?

            };
         }

オブジェクトは次にインスタンス化されます

var myNowNextView = new nowNextView();

次に、メソッドを実行します。

myNowNextView.update(stuff);

プログラムの本体内で問題を説明しようとしました。コードにエラーは発生せず、try/catch を実行する必要がありましたが、メソッドが見つからないと不承不承に告げられました。なんというかデザインがおかしい?私はこれを行うことができますか?よろしくお願いします、スティーブ

4

2 に答える 2

0

javascriptでクロージャを少し勉強することで本当に恩恵を受けることができると思います。従来のオブジェクト指向アプローチをjsオブジェクトに適用しようとしているようですが、期待した結果が得られません。

クロージャを簡単に使用する方法については、この投稿を読むことをお勧めします:http: //howtonode.org/why-use-closure

このようなもの:

<html>
<head>
<script type="text/javascript"> 

  function createScheduleItemView(myId){

    var _show = false

    return {
        setShow : function setShow(show) {
            _show = show
        }, 
        update : function update(){
            document.getElementById( myId +'-title').innerHTML = _show.title;

        }
      }

   }

   function createNowNextView(){

       var _now  = createScheduleItemView('now');
       var _next = createScheduleItemView('next');

       return { 
            publicVar : "Example",
            update : function update(args) {
                var myshow=args[0];
                _now.setShow(myshow.now)
                _now.update();
                _next.setShow(myshow.next)
                _next.update();
            }

        };
     }

   function runIt() {
        nowNextView = createNowNextView()
        args = []
        showArgs = {
            now : {title : "Beauty and the Beast"},
            next : {title: "I did it myyyyyy way"}
        }
        args.push(showArgs)
        nowNextView.update(args)

        //Private variables can not be accessed
        //console.log(nowNextView._now)
        //undefined
        //
        //But anything you return in the object is public
        //console.log(nowNextView.publicVar)
        //Example

   }


</script>
</head>
<body onload="runIt()">

<h3>Now Showing:</h3>
<p id="now-title"></p>

<h3>Up Next:</h3>
<p id="next-title"></p>

 </body>
 </html>
于 2011-03-09T21:09:25.587 に答える
0
function scheduleItemView(myId){
this.update = function(show){
  document.getElementById(this.id+'-title').innerHTML = show.title +": "+ show.startDate;
  document.getElementById(this.id+'-title-overlay').innerHTML = show.title +": "+ show.startDate;
  document.getElementById(this.id+'-description').innerHTML = truncate(show.description,190);
  document.getElementById(this.id+'-time-start').innerHTML = show.startTime;
  document.getElementById(this.id+'-time-end').innerHTML = show.endTime;
};
this.id=myId;
}

function nowNextView(){
var myshow=args[0];
var scope = this;
this.now = new scheduleItemView('now');
this.next = new scheduleItemView('next');
this.update = function(type,args){
      scope.now.update(myshow.now);
      scope.next.update(myshow.next);
};
}
于 2011-03-09T21:13:50.150 に答える