UNPKG

can

Version:

MIT-licensed, client-side, JavaScript framework that makes building rich web applications easy.

530 lines (348 loc) 14.3 kB
<!DOCTYPE html> <!--#################################################################### THIS IS A GENERATED FILE -- ANY CHANGES MADE WILL BE OVERWRITTEN INSTEAD CHANGE: source: [object Object] @function can.List.prototype.splice ######################################################################## --> <html lang="en"> <head> <meta charset="utf-8"> <title>CanJS - splice</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <link rel="stylesheet" type="text/css" href="./static/bundles/bit-docs-site/static.css"> <link rel="shortcut icon" sizes="16x16 24x24 32x32 48x48 64x64" href="/docs/images/canjs_favicon.ico"> <link rel="apple-touch-icon" sizes="57x57" href="../docs/images/canjs_favicon_57x57.png"> <link rel="apple-touch-icon-precomposed" sizes="57x57" href="../docs/images/canjs_favicon_57x57.png"> <link rel="apple-touch-icon" sizes="72x72" href="../docs/images/canjs_favicon_72x72.png"> <link rel="apple-touch-icon" sizes="114x114" href="../docs/images/canjs_favicon_114x114.png"> <link rel="apple-touch-icon" sizes="120x120" href="../docs/images/canjs_favicon_128x128.png"> <link rel="apple-touch-icon" sizes="144x144" href="../docs/images/canjs_favicon_144x144.png"> <link rel="apple-touch-icon" sizes="152x152" href="../docs/images/canjs_favicon_152x152.png"> <meta content="yes" name="apple-mobile-web-app-capable"> <meta name="apple-mobile-web-app-status-bar-style" content="white-translucent"> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-2302003-11', 'auto'); ga('send', 'pageview'); </script> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger"/> <label for="nav-trigger">Menu</label> <div id="everything"> <div id="left" class="column"> <div class="top-left"> <div class="brand"> <div class="logo"> <a href="../index.html" alt="CanJS"></a> <div class="dropdown project-dropdown"> <a href="https://donejs.com/">DoneJS</a> <a href="http://stealjs.com/">StealJS</a> <a href="http://jquerypp.com/">jQuery ++</a> <a href="https://funcunit.com/">FuncUnit</a> <a href="http://documentjs.com/">DocumentJS</a> </div> </div> <div class="version"> <div class="version-number"> 3.0.0 </div> <div class="dropdown version-dropdown"> <a href="https://v2.canjs.com">2.3.27</a> </div> </div> </div> <div class="search-bar"> <p> &nbsp; </p> </div> </div> <div class="bottom-left"> <div class="social-side-container"> <ul class="social-side"> <li> <a class="header-mobile github" href="https://github.com/canjs/canjs" target="_blank"><img class="social-icon-small" src="../docs/images/github.png">Github</a> </li> <li> <a class="header-mobile twitter" href="https://twitter.com/canjs" target="_blank"><img class="social-icon-small" src="../docs/images/twitter.png">Twitter</a> </li> </ul> <ul class="social-side"> <li> <a class="header-mobile" href="https://gitter.im/canjs/canjs" target="_blank">Chat</a> </li> <li> <a class="header-mobile" href="http://forums.donejs.com/c/canjs" target="_blank">Forum</a> </li> </ul> </div> <ul> <li> <span>prototype</span> <ul> <li class=" "> <a class="function" href="can.List.prototype.concat.html" title="Merge many collections together into a List."> concat </a> </li> <li class=" "> <a class="function" href="can.List.prototype.forEach.html" title="Call a function for each element of a List."> forEach </a> </li> <li class=" "> <a class="function" href="can.List.prototype.indexOf.html" title="Look for an item in a List."> indexOf </a> </li> <li class=" "> <a class="function" href="can.List.prototype.join.html" title="Join a List&#x27;s elements into a string."> join </a> </li> <li class=" "> <a class="function" href="can.List.prototype.pop.html" title="Remove an element from the end of a List."> pop </a> </li> <li class=" "> <a class="function" href="can.List.prototype.push.html" title="Add elements to the end of a list."> push </a> </li> <li class=" "> <a class="function" href="can.List.prototype.replace.html" title="Replace all the elements of a List."> replace </a> </li> <li class=" "> <a class="function" href="can.List.prototype.reverse.html" title="Reverse the order of a List."> reverse </a> </li> <li class=" "> <a class="function" href="can.List.prototype.shift.html" title="Remove en element from the front of a list."> shift </a> </li> <li class=" "> <a class="function" href="can.List.prototype.slice.html" title="Make a copy of a part of a List."> slice </a> </li> <li class="current parent expanded"> <a class="function" href="can.List.prototype.splice.html" title="Insert and remove elements from a List."> splice </a> </li> <li class=" "> <a class="function" href="can.List.prototype.unshift.html" title="Add elements to the beginning of a List."> unshift </a> </li> </ul> </li> <li> <span>static</span> <ul> <li class=" "> <a class="property" href="can.List.Map.html" title="Specify the Map type used to make objects added to this list observable."> can.List.Map </a> </li> </ul> </li> </ul> </div> </div> <div id="right" class="column"> <div class="top-right"> <div class="top-right-top"> <ul class="top-right-bitovi"> <li class="dropdown"> <a href="http://bitovi.com" class="bitovi icon-bits">Bitovi</a> <ul class="dropdown-menu"> <li><a href="http://bitovi.com">Bitovi.com</a></li> <li><a href="http://bitovi.com/blog/">Blog</a></li> <li><a href="http://bitovi.com/consulting/">Consulting</a></li> <li><a href="http://bitovi.com/training/">Training</a></li> <li><a href="http://bitovi.com/open-source/">Open Source</a></li> </ul> </li> </ul> <div class="brand"> <div class="logo"> <a href="../index.html" alt="CanJS"></a> </div> </div> <ul class="top-right-links"> <li> <a href="https://gitter.im/canjs/canjs">Chat</a> </li> <li> <a href="http://forums.donejs.com/c/canjs">Forum</a> </li> <li> <a class="github-button nav-social" href="https://github.com/canjs/canjs" data-count-href="/canjs/canjs/stargazers" data-count-api="/repos/canjs/canjs#stargazers_count">Star</a> </li> <li> <a href="https://twitter.com/canjs" class="twitter-follow-button nav-social" data-show-count="true" data-show-screen-name="false">Follow @canjs</a><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script> </li> </ul> </div> <div class="breadcrumb"> <li><a href="can.List.html">can.List</a></li> / <li><a href="can.List.prototype.splice.html">splice</a></li> <li class="breadcrumb-dropdown">/ <a> On this page</a> <ul class="on-this-page"></ul> </li> <div class="nav-toggle" title="Back to top"></div> </div> </div> <div class="bottom-right"> <article> <section class="title"> <div class="page-type"> <h1>splice</h1> <div>function</div> </div> <section class="description"> <p>Insert and remove elements from a List.</p> </section> </section> <section class="on-this-page-table"> </section> <section class="title-footer"> <ul class="title-links"> <!-- <li><a href="#">docco</a></li> --> <li><a href="//github.com/canjs/canjs/tree/v3.0.0/node_modules/can-list/can-list.js#L261">source</a></li> <!-- <li><a href="#">download</a></li> --> <!-- <li><a href="#">tests</a></li> --> </ul> </section> <div class="signature"> <h2 class="signature-title"> <code>list.splice(index[, howMany[, ...newElements]])</code> </h2> <div class="parameters"> <h3 class="parameters-title">Parameters</h3> <ol> <li><b>index</b> <code>{Number}</code>: <p>where to start removing or inserting elements</p> </li> <li><b>howMany</b> <code>{Number}</code>: <p>the number of elements to remove If <em>howMany</em> is not provided, <code>splice</code> will remove all elements from <code>index</code> to the end of the List.</p> </li> <li><b>newElements</b> <code>{*}</code>: <p>elements to insert into the List</p> </li> </ol> </div> <div class="returns"> <h3 class="returns-title">Returns</h3> <p> <code>{Array}</code>: <p>the elements removed by <code>splice</code></p> </p> </div> </div> <section class="body"> <p><code>splice</code> lets you remove elements from and insert elements into a List.</p> <p>This example demonstrates how to do surgery on a list of numbers:</p> <pre><code>var list = new can.List([0, 1, 2, 3]); // starting at index 2, remove one element and insert 'Alice' and 'Bob': list.splice(2, 1, 'Alice', 'Bob'); list.attr(); // [0, 1, 'Alice', 'Bob', 3] </code></pre> <h2>Events</h2> <p><code>splice</code> causes the List it's called on to emit <em>change</em> events, <em>add</em> events, <em>remove</em> events, and <em>length</em> events. If there are any elements to remove, a <em>change</em> event, a <em>remove</em> event, and a <em>length</em> event will be fired. If there are any elements to insert, a separate <em>change</em> event, an <em>add</em> event, and a separate <em>length</em> event will be fired.</p> <p>This slightly-modified version of the above example should help make it clear how <code>splice</code> causes events to be emitted:</p> <pre><code>var list = new can.List(['a', 'b', 'c', 'd']); list.bind('change', function(ev, attr, how, newVals, oldVals) { console.log('change: ' + attr + ', ' + how + ', ' + newVals + ', ' + oldVals); }); list.bind('add', function(ev, newVals, where) { console.log('add: ' + newVals + ', ' + where); }); list.bind('remove', function(ev, oldVals, where) { console.log('remove: ' + oldVals + ', ' + where); }); list.bind('length', function(ev, length) { console.log('length: ' + length + ', ' + this.attr()); }); // starting at index 2, remove one element and insert 'Alice' and 'Bob': list.splice(2, 1, 'Alice', 'Bob'); // change: 2, 'remove', undefined, ['c'] // remove: ['c'], 2 // length: 5, ['a', 'b', 'Alice', 'Bob', 'd'] // change: 2, 'add', ['Alice', 'Bob'], ['c'] // add: ['Alice', 'Bob'], 2 // length: 5, ['a', 'b', 'Alice', 'Bob', 'd'] </code></pre> <p>More information about binding to these events can be found under [can.List.attr attr].</p> </section> <script type="text/javascript"> var docObject = {"type":"function","name":"can.List.prototype.splice","parent":"can.List.prototype","src":{"line":261,"codeLine":324,"path":"node_modules/can-list/can-list.js"},"description":"Insert and remove elements from a List. ","title":"splice","signatures":[{"code":"list.splice(index[, howMany[, ...newElements]])","description":"","params":[{"types":[{"type":"Number"}],"name":"index","description":"where to start removing or inserting elements\n"},{"types":[{"type":"Number"}],"optional":true,"name":"howMany","description":"the number of elements to remove\nIf _howMany_ is not provided, `splice` will remove all elements from `index` to the end of the List.\n"},{"types":[{"type":"*"}],"name":"newElements","description":"elements to insert into the List\n"}],"returns":{"types":[{"type":"Array","options":[]}],"description":"the elements removed by `splice`\n"}}],"_curParam":{"types":[{"type":"*"}],"name":"newElements","description":"elements to insert into the List\n"},"_curReturn":{"types":[{"type":"Array","options":[]}],"description":"the elements removed by `splice`\n"},"comment":" ","pathToRoot":".."}; </script> </article> <footer><p>CanJS is part of <a href="http://donejs.com" target="_blank">DoneJS</a>. Created and maintained by the core <a href="https://donejs.com/About.html#section=section_Team" target="_blank">DoneJS team</a> and <a href="http://bitovi.com" target="_blank">Bitovi</a>. <strong>Currently 3.0.0.</strong></p></footer> </div> </div> </div> <script> steal = { instantiated: { "bundles/bit-docs-site/static.css!$css" : null } }; </script> <script type='text/javascript' data-main="bit-docs-site/static" src="./static/node_modules/steal/steal.production.js"></script> <script async defer src="https://buttons.github.io/buttons.js"></script> </body> </html>