can-define
Version:
Create observable objects with JS dot operator compatibility
74 lines (51 loc) • 2.16 kB
Markdown
can-define/list/list.prototype.set set
can-define/list/list.prototype
Sets an item or property or items or properties on a list.
`list.set(index, value)`
Sets the item at `index`. Typically, [can-define/list/list::splice] should be used instead.
```js
import {DefineList} from "can";
const list = new DefineList(["A","B"]);
list.set(2,"C");
console.log(list[2]); //-> "C"
```
Splice should be used because it will trigger a [can-define/list/list/LengthEvent]
event. If you are using `.set(index, value)`, you should make sure to use `.get(index)`
when reading values from the array. If you use `.splice()`, you can use `list[index]`
to read values from the array.
{Number} index A numeric position in the list.
{*} value The value to add to the list.
{can-define/list/list} The list instance.
`list.set(prop, value)`
Sets the property at `prop`. This should be used when the property
isn't already defined.
```js
import {DefineList} from "can";
const list = new DefineList(["A","B"]);
list.set("count",1000);
console.log(list.get("count")); //-> 1000
```
{Number} prop A property name.
{} value The value to add to the list.
{can-define/list/list} The list instance.
`list.set(newProps)`
<section class="warnings">
<div class="deprecated warning">
<h3>Deprecated 3.10.1</h3>
<div class="signature-wrapper">
<p>Passing an {Object} to <code>.set</code> has been deprecated in favor of <a href="list.prototype.assign.html">assign</a> or <a href="list.prototype.update.html">update</a>. <code>list.set(index, value)</code> is <em>not</em> deprecated.</p>
</div>
</div>
</section>
Updates the properties on the list with `newProps`.
```js
import {DefineList} from "can";
const list = new DefineList(["A","B"]);
list.set({count: 1000, skip: 2});
console.log(list.get("count")); //-> 1000
```
{Object} newProps An object of properties and values to set on the list.
{can-define/list/list} The list instance.