Is there a name for a method that destroys an argument, like a destructive method when the caller changes state?

Asked 1 years ago, Updated 1 years ago, 37 views

obj.fn(x)
What is the name of the function that changes the value of the argument x before and after calling?

Ruby is
obj.reverse!
The one whose state of obj changes in is called a destructive method.It's like this.

javascript ruby

2022-09-30 14:50

4 Answers

obj.fn(x)

prototype

if you're talking about calling this function
obj.fn(x)
fn(obj,x);

For functions that are destroyed when passed to a function no matter how you call them, see
I've never heard of a specific name in JavaScript, so wouldn't it be okay to use the destructive method?
I don't feel uncomfortable even if it's called a "destructive method" in JavaScript.
Sometimes it is called or described.
However, JavaScript cannot add "!" after the function, so
It may not be used for problems prior to the original term.


2022-09-30 14:50

Do you mean reference delivery?It is not limited to javascript, but most languages.
It's not so much a function as a reference.

 var ref = ["ABC"; // Reference is substituted

alert(ref[0]); // Display ABC
bbb(ref); // Reference Pass
alert(ref[0]); // Display XYZ

function bbb(aaa){// Reference passing
   aaa[0] = "XYZ";
}


2022-09-30 14:50

As far as the ECMAScript specification goes, it doesn't seem to make that distinction for each function, and it doesn't seem to be a term.

If you want to name it, I think it's a non-pure function.JavaScript methods are part of a function and are included in it.

A non-pure function is simply a function that is not a pure function.In programming (especially functional programming), it is important to ensure that the function is pure, i.e., that it has reference transparency and has no side effects.Therefore, the distinction between pure and non-pure is valuable.

However, if you say "non-pure function", it can be one or more of the following:

I think the only thing I want to ask this time is 2. But there is no point in distinguishing only that, so I don't think there is a term to describe only 2.

Ruby's destructive method is always a "non-genuine function" to destroy the receiver.However, Ruby's usage is often used to distinguish receivers from non-destructive methods that do not destroy them, and the absence of ! does not necessarily mean that they are non-destructive or pure.Also, ! only indicates that it is dangerous, unusual, and not because it is a destructive method.Most of the built-in class destructive methods fall under 1.

Finally, there is a caveat.It is very difficult to determine whether JavaScript is really a "pure function."For example, look at the following code:

class NumberCalc{
    constructor(val) {this.val=val;}
    add(num) {return num+this.val;}
}
const nc = new NumberCalc(42);
const x = {val:1};
x.valueOf=function() {return this.val++};
console.log(x.val);
console.log(nc.add(x));
console.log(x.val);

NumberCalc.prototype.add does not appear to make any difference to the object of the argument, but you will see that the contents of x changed before and after the nc.add(x) call.In this case, is nc.add a pure function?Now that I've thought about it, I feel that making these distinctions in JavaScript itself is a waste of time.

Please refer to the following article as well.
Is that JavaScript really a pure function?|Programming | POSTD


2022-09-30 14:50

I don't think the following is JavaScript's unique term...

parser.

  • JSON.parse
  • parseInt
  • parseFloat

Serializer.

  • JSON.stringify

Type conversion.

  • Number()
  • String()
  • Boolean()
  • Object()
  • Array.from()

A function that changes the state/value of an object (no proper noun).

  • Object.defineProperty
  • Object.defineProperties()
  • Object.seal()
  • Object.assign()
  • Object.setPrototypeOf()


2022-09-30 14:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.