JavaScript ES6对Proxy的原生支持的一个例子:开发人员学习额外的编程语言

  • 2020 年 3 月 26 日
  • 筆記

源代码:

<html>  <script>    function Employee(name){  	this.name = name;  };    Employee.prototype.work = function(language){  	console.log(this.name + " is developing with: " + language);  }    let jerry = new Employee("Jerry");    function hireEmployee(employee, language){      employee.work(language);  }    hireEmployee(jerry, "JavaScript");    var proxyLogic = {      get: function(target, name) {      	if( name == "work"){      		var oriFun = target[name].bind(target);      		return function(language){      			oriFun(language);      			console.log("and also study other language in spare time");      		}      	}      }  };    var jerryProxy = new Proxy(jerry, proxyLogic );    hireEmployee(jerryProxy, "JavaScript");    </script>  </html>

使用代理模式,使得Jerry作为一个ABAP开发人员被雇佣的时候,在业余时间里,也能自学其他的编程语言: