Prototypes



  • function IO() {
        this.foo = function() {
            alert("FOO");
        }
    }
    IO.prototype.bar = function() {
      alert("BAR");
    }
    var IO = new IO();
    console.log(IO);
    

    console.log

    What's the difference between announcements through this and prototype?



  • In this case, the function will be created every time the facility is established, and the variables declared inside the designer may be used in the circuit.

    function IO() {
      var _private = 10;
      this.foo = function() {
        _private = 15;//OK
        alert("FOO");
      }
    }
    

    In this case, the functions are created once and have no access to locally declared variables

    IO.prototype.bar = function() {
      var some = _private + 15;// Uncaught ReferenceError: _private is not defined
      alert("BAR");
    }
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2