jQuery Wrapper中的对象

时间:2015-08-29 00:04:18

标签: javascript jquery

我正在尝试检查使用console.log创建的对象的属性和方法。例如,下面的hardToFind对象是在包装器$(function(){});中创建的,但我似乎无法在窗口对象下找到此对象,或其他任何地方。有人可以帮忙吗?

function easyToFind() {
  console.log("I am easy to find")
}

$(function() {
  //can't find this hardToFind object in console.log(window)!

  var hardToFind = new HardToFind();

  function HardToFind() {
    this.projectName = "New Project";

  }
});

2 个答案:

答案 0 :(得分:2)

那是因为window是一个局部变量。外部上下文无法访问变量。如果要定义全局变量,请将其定义为window.hardToFind = new HardToFind(); 对象的属性:

var

或删除NOTE: Values are Little-Endian! (right-to-left) base1: 0(0) + 00(0) = 000(0) base2: 01(2) + 1(1) = 110(3) base2: 11(3) + 01(2) = 101(5) base2: 11(3) + 011(6) = 1001(9) base16: 0A(160) + 16(97) = 101(257) base32: 0R(864) + 15(161) = 101(1025) 关键字。

答案 1 :(得分:0)

检查此示例。



// jQuery scope...
$(function() {
  function HardToFind() {
    this.projectName = "New Project"; // Private property.
    this.testMethod = function() // Private method.
      {
        alert(this.projectName); // You can show the projectName property.
      };
  }

  function easyToFind() {
    var hardToFind = new HardToFind();
    hardToFind.projectName = "Another test Project"; // Initializing with other value.
    return hardToFind; // Returning the new value.
  }

  function showAlert() {
    var hardToFind = new HardToFind();
    hardToFind.testMethod(); // hardToFind is instance of the class HardToFind() (OOP). So you can call the private method of the object «hardToFind».
  }

  /* Excuting the methods by default. */
  console.log(easyToFind());
  showAlert(); // Show Alert window, to test private method of HardToFind().
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

相关问题