<commit_before>@function can-define.types.set set
@parent can-define.behaviors

Specify what happens when a property value is set.

@signature `set( [newVal,] [resolve] )`


> NOTE: Instead of using `set` to set the values of other properties, use the [can-define.types.value] behavior.

A set function defines the behavior of what happens when a value is set on an
instance. It is typically used to:

 - Add or update other properties as side effects
 - Coerce the set value into an appropriate action

The behavior of the setter depends on the number of arguments specified. This means that a setter like:

```js
{
	prop: {
		set: function() {}
	}
}
```

behaves differently than:

```js
{
	prop: {
		set: function( newVal ) {}
	}
}
```

@param {*} [newVal] The [can-define.types.type type function] coerced value the user intends to set on the
instance.

@param {function(*)} [resolve(newValue)] A callback that can set the value of the property
asynchronously.

@return {*|undefined} If a non-undefined value is returned, that value is set as
the attribute value.


If an `undefined` value is returned, the behavior depends on the number of
arguments the setter declares:

 - If the setter _does not_ specify the `newValue` argument, the property value is set to the type converted value.
 - If the setter specifies the `newValue` argument only, the attribute value will be set to `undefined`.
 - If the setter specifies both `newValue` and `resolve`, the value of the property will not be
   updated until `resolve` is called.


@body

## Use

A property's `set` function can be used to customize the behavior of when an attribute value is set.  Let'ss see some common cases:

#### Side effects

The following makes setting a `page` property update the `offset`:


```js
{
	page: {
		set: function( newVal ) {
			this.offset =  ( parseInt( newVal ) - 1 ) * this.limit;
		}
	}
}
```

The following makes changing `makeId` un-define the `modelId` property:

```
{
	makeId: {
	    set: function(newValue){
	        // Check if we are changing.
	        if(newValue !== this.makeId) {
	            this.modelId = undefined;
	        }
	        // Must return value to set as we have a `newValue` argument.
	        return newValue;
	    }
	}
}
```

#### Asynchronous Setter

The following shows an async setter:

```js
{
	prop: {
		set: function( newVal, setVal ) {
			$.get( "/something", {}, setVal );
		}
	}
}<commit_msg>added syntax highlighting to Use's second example<commit_after>@function can-define.types.set set
@parent can-define.behaviors

Specify what happens when a property value is set.

@signature `set( [newVal,] [resolve] )`


> NOTE: Instead of using `set` to set the values of other properties, use the [can-define.types.value] behavior.

A set function defines the behavior of what happens when a value is set on an
instance. It is typically used to:

 - Add or update other properties as side effects
 - Coerce the set value into an appropriate action

The behavior of the setter depends on the number of arguments specified. This means that a setter like:

```js
{
	prop: {
		set: function() {}
	}
}
```

behaves differently than:

```js
{
	prop: {
		set: function( newVal ) {}
	}
}
```

@param {*} [newVal] The [can-define.types.type type function] coerced value the user intends to set on the
instance.

@param {function(*)} [resolve(newValue)] A callback that can set the value of the property
asynchronously.

@return {*|undefined} If a non-undefined value is returned, that value is set as
the attribute value.


If an `undefined` value is returned, the behavior depends on the number of
arguments the setter declares:

 - If the setter _does not_ specify the `newValue` argument, the property value is set to the type converted value.
 - If the setter specifies the `newValue` argument only, the attribute value will be set to `undefined`.
 - If the setter specifies both `newValue` and `resolve`, the value of the property will not be
   updated until `resolve` is called.


@body

## Use

A property's `set` function can be used to customize the behavior of when an attribute value is set.  Let's see some common cases:

#### Side effects

The following makes setting a `page` property update the `offset`:


```js
{
	page: {
		set: function( newVal ) {
			this.offset =  ( parseInt( newVal ) - 1 ) * this.limit;
		}
	}
}
```

The following makes changing `makeId` un-define the `modelId` property:

```js
{
	makeId: {
	    set: function(newValue){
	        // Check if we are changing.
	        if(newValue !== this.makeId) {
	            this.modelId = undefined;
	        }
	        // Must return value to set as we have a `newValue` argument.
	        return newValue;
	    }
	}
}
```

#### Asynchronous Setter

The following shows an async setter:

```js
{
	prop: {
		set: function( newVal, setVal ) {
			$.get( "/something", {}, setVal );
		}
	}
}
{'fraction_non_alphanumeric': 0.10006138735420503, 'fraction_numerical': 0.0004092490280335584, 'mean_word_length': 3.376007162041182, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1601025', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1432, 'n_words': 645}