Javascript `это` не работает, как я думал? - PullRequest
0 голосов
/ 20 января 2009

Я адаптирую довольно простую функцию js в класс. Во всяком случае, в основном это просто создает плавающий контейнер над главной страницей. Я знаю, что он неполный, но я уже набираю его и продолжаю ловить, пытаясь вызвать функцию close (). Firefox возвращает this.sdiv не определено. Я не понимаю, как это может быть в случае, когда close () является методом Pop, а sdiv определен в первой строке класса Pop?

function Pop( wpx,hpx ){

  Pop.prototype.sdiv;
  Pop.prototype.shadow;
  Pop.prototype.pdiv;

  // start with the invisible screen, which
  // covers the main page
  this.sdiv = document.createElement("DIV")
  this.sdiv.className = "popScreen";
  this.sdiv.id = "popScreen";
  // this screen covers the full document, so 
  // base dimensions on the document size
  this.sdiv.style.width = document.body.clientWidth + "px";
  this.sdiv.style.height = document.body.clientHeight + "px"; 
  document.body.appendChild( this.sdiv );

  // attach drop shadow
  this.shadow = document.createElement("DIV");
  this.shadow.className = "popShadow";
  this.shadow.style.width = wpx + "px";
  this.shadow.style.height = hpx + "px";
  this.shadow.style.left = ( ( window.innerWidth / 2 ) - ( wpx / 2 )  ) + "px";
  this.shadow.style.top = ( ( window.innerHeight / 2 ) - ( hpx / 2 ) ) + "px";
  document.body.appendChild( this.shadow );

  this.pdiv = document.createElement("DIV");
  this.pdiv.className = "pop";
  this.pdiv.id = "pop";
  this.pdiv.style.position = "absolute";
  this.pdiv.style.width = wpx + "px";
  this.pdiv.style.height = hpx + "px";
  this.shadow.appendChild( this.pdiv );

  // bind an event to the screen div so that when it is clicked
  // the Pop dialogue is closed and the user is return to the main page
  $("div#popScreen").click( function( ){
    Pop.prototype.close( );
  } );

  Pop.prototype.go = function( url, method, data ){ 
    if( method == null )
      $("div#pop").load( url );
  }

  Pop.prototype.close = function( ){
      this.sdiv.parentNode.removeChild( this.sdiv );
      this.shadow.parentNode.removeChild( this.shadow );
      this.pdiv.parentNode.removeChild( this.pdiv );
  }

} 

Заранее спасибо за любую помощь

Ответы [ 2 ]

3 голосов
/ 20 января 2009

Вы не можете использовать Pop.prototype.close(), чтобы закрыть все Pop экземпляры. Вместо этого для каждого экземпляра Pop, который вы создали с помощью оператора new, вам нужно вызвать popInstance.close().

1 голос
/ 20 января 2009

this в javascript работает совсем иначе по сравнению с другими языками.

Ваша ошибка здесь:

  Pop.prototype.close = function( ){
      this.sdiv.parentNode.removeChild( this.sdiv );
      this.shadow.parentNode.removeChild( this.shadow );
      this.pdiv.parentNode.removeChild( this.pdiv );
  }

В этой функции this, вероятно, ссылается на окно (установите точку останова и посмотрите на firebug)

Возможно, что-то в этом роде будет работать.

 var parent = this;
 Pop.prototype.close = function(){
      parent.sdiv.parentNode.removeChild( this.sdiv );
      parent.shadow.parentNode.removeChild( this.shadow );
      parent.pdiv.parentNode.removeChild( this.pdiv );
  }
...