javascript - How to curry jQuery methods -- which `this` value will pass the jQuery object? -


if want create jquery function using function.prototype.bind, i.e. need wrapping function, this value supply? following trivial example not seem work:

// e.g.: $.fn.outerheight() argument true gets height including margin  $.fn.marginboxheight = $.fn.outerheight.bind($.fn, true); 

$.fn wrong choice here this argument. should be, function have access jquery internal methods, if needed?

jquery.proxy performs currying follows:

qunit.test( "jquery.proxy", function( assert ) {      var thisobject = { foo: "bar", method: test };      // jquery 1.9 improved currying `this` object context     fn = function() {         assert.equal( array.prototype.join.call( arguments, "," ), "arg1,arg2,arg3", "args passed" );         assert.equal( this.foo, "bar", "this-object passed" );     };     cb = jquery.proxy( fn, null, "arg1", "arg2" );     cb.call( thisobject, "arg3" ); } ); 

qunit.test( "jquery.proxy", function( assert ) {  	assert.expect( 9 );    	var test2, test3, test4, fn, cb,  		test = function() {  			assert.equal( this, thisobject, "make sure scope set properly." );  		},  		thisobject = { foo: "bar", method: test };    	// make sure normal works  	test.call( thisobject );    	// basic scoping  	jquery.proxy( test, thisobject )();    	// take on  	jquery.proxy( thisobject, "method" )();    	// make sure doesn't freak out  	assert.equal( jquery.proxy( null, thisobject ), undefined, "make sure no function returned." );    	// partial application  	test2 = function( ) {  		assert.equal( a, "pre-applied", "ensure arguments can pre-applied." );  	};  	jquery.proxy( test2, null, "pre-applied" )();    	// partial application w/ normal arguments  	test3 = function( a, b ) {  		assert.equal( b, "normal", "ensure arguments can pre-applied , passed usual." );  	};  	jquery.proxy( test3, null, "pre-applied" )( "normal" );    	// test old syntax  	test4 = { "meth": function( ) {  		assert.equal( a, "boom", "ensure old syntax works." );  	} };  	jquery.proxy( test4, "meth" )( "boom" );    	// jquery 1.9 improved currying `this` object  	fn = function() {  		assert.equal( array.prototype.join.call( arguments, "," ), "arg1,arg2,arg3", "args passed" );  		assert.equal( this.foo, "bar", "this-object passed" );  	};  	cb = jquery.proxy( fn, null, "arg1", "arg2" );  	cb.call( thisobject, "arg3" );  } );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.4.0.css">  <script src="https://code.jquery.com/qunit/qunit-2.4.0.js"></script>  <div id="qunit"></div>  <div id="qunit-fixture"></div>

the simplest example be:

var foo = jquery.proxy(function(){return this('html:first')}, jquery)  console.log(foo() )
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

references


Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

How to fix java warning for "The value of the local variable is not used " -

Google sheets equipment borrowing system -