javascript - Creating jquery plugin for objects -


i want create jquery plugin array splice(opposite of push() function).but error
uncaught typeerror: allfiles.pull not function
here code:

$.fn.pull = function (index) {  	this.splice(index-1,1);  }    var allfiles = ['1','2','3','4','5'];  $("div").html(allfiles);  allfiles.pull(1);  $("div").html(allfiles);
.first:before{  content:'before:';  }  .second:before{  content:'after:';  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="first"></div>  <div class="second"></div>

because $.fn.pull extends jquery function list, not add method object array. do array.prototype.pull. in original code array.pull (allfiles.pull) undefined, because you've never declared it.

i've changed code , wrapped array jquery function: $(allfiles). should give need.

$.fn.pull = function (index) {  	this.splice(index-1,1);    return this.toarray();  }    var allfiles = ['1','2','3','4','5'];  $("div.first").html(allfiles);  allfiles = $(allfiles).pull(1);  $("div.second").html(allfiles);
.first:before{  content:'before:';  }  .second:before{  content:'after:';  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="first"></div>  <div class="second"></div>

stack snippet array.prototype.pull (not recommended*)

array.prototype.pull = function (index) {  	return this.splice(index-1,1);  }    var allfiles = ['1','2','3','4','5'];  $("div.first").html(allfiles);  allfiles.pull(1);  $("div.second").html(allfiles);
.first:before{  content:'before:';  }  .second:before{  content:'after:';  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="first"></div>  <div class="second"></div>

* altering original object of javascript such array custom functions not recommended. behaviour can change or external scripts jquery rely on same names. pull pretty generic. @ least use destinctive name myproject_pull.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -