Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách truyền đối tượng (Object) làm tham số trong hàm JavaScript

Để truyền object làm tham số, dùng this và prototype hoặc đơn giản truyền object literal/reference.

Ví dụ

<html>
  <head>
    <script>
      var Func = function(param1) {
        this.param1 = param1;
      };
      Func.prototype.display = function() {
        return this.param1;
      };
      function display(val) {
        document.write(val());
      }
      var res = new Func(99);
      display(res.display.bind(res));
    </script>
  </head>
</html>