JavaScript 編程範式:of 接口,告別手動new對象.

var of_interface = {
		// valueOf: function (...d) {
		// 	return new this(...d);
		// },
		// of: function (...d) {
		// 	return this.valueOf(...d);
		// },
		valueOf: function () {
			var that = this;
			for (var i = 0; i < arguments.length; i++) {
				that = that.bind(this/*not useful*/, arguments[i]);
			}
			return new that();
		},
		of: function () {
			var that = this;
			for (var i = 0; i < arguments.length; i++) {
				that = that.bind(this/*not useful*/, arguments[i]);
			}
			return new that();
		},


	};
function Clazz(){
}

shallowCopyObj(Clazz, of_interface);

//you can
var obj = Clazz.of();//OK

function A(a,b,c){}

shallowCopyObj(A, of_interface);

//you can
var a = A.of(1,2,3);//OK


不在使用new,直接調用即可,可傳遞任何參數,任意多個參數.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章