JavaScript 原型和修改原始对象

huangapple go评论112阅读模式
英文:

Javascript prototype and modify original object

问题

You can update the original object by modifying the prototype method to reverse the array in place. Here's the modified code:

  1. Array.prototype.myReverse = function() {
  2. let length = this.length;
  3. for (let i = 0; i < Math.floor(length / 2); i++) {
  4. let temp = this[i];
  5. this[i] = this[length - 1 - i];
  6. this[length - 1 - i] = temp;
  7. }
  8. return this;
  9. }
  10. let a = [9, 0, 3, 4];
  11. console.log("Before ", a); // [9, 0, 3, 4]
  12. console.log("reverse - ", a.myReverse()); // [4, 3, 0, 9]
  13. console.log("After ", a); // [4, 3, 0, 9]

This updated myReverse function reverses the original array in place, modifying the original object.

英文:

How can we update passed object in prototype? I have created similar prototype as Array.reverse, but how I can modify original object?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. Array.prototype.myReverse = function() {
  2. let arr = [];
  3. for (let i = 0; i &lt; this.length; i++) {
  4. arr.unshift(this[i]);
  5. }
  6. return arr;
  7. }
  8. let a = [9, 0, 3, 4];
  9. console.log(&quot;Before &quot;, a); // [9, 0, 3, 4]
  10. console.log(&quot;reverse - &quot;, a.myReverse()); // [4, 3, 0, 9]
  11. //not modifying original object , how to modify original object
  12. console.log(&quot;After &quot;, a); // [9, 0, 3, 4]

<!-- end snippet -->

I have check few examples but I didnt get how to update orginal object inside prototype
How can we crate a prototype which will update original object (Careful: reverse is destructive -- it changes the original array.) If not possible for predefined Array then how we can create similar MyArray to write a prototype for updating original object.

答案1

得分: 2

你不能直接赋值给 this,但你仍然可以更改其属性。因此,保持你发布的代码风格,你可以按照以下方式进行操作:

  1. Array.prototype.myReverse = function() {
  2. let arr = [...this]
  3. for (let i = 0; i < this.length; i++) {
  4. this[i] = arr.pop()
  5. }
  6. }
英文:

You cannot assign this directly, but you can still change its properties. So keeping in the style of the code you posted, you could do something along the lines of:

  1. Array.prototype.myReverse = function() {
  2. let arr = [...this]
  3. for (let i = 0; i &lt; this.length; i++) {
  4. this[i] = arr.pop()
  5. }
  6. }

答案2

得分: 0

如果你想原地反转数组(并且如果你愿意,返回它),你可以创建一个临时栈,通过弹出数组的头部直到它为空,然后将临时元素推入,就好像它们是在队列中一样。

  1. 临时栈:
  • ARR→POP ⇒ TMP→PUSH(LILO)
  • ARR→SHIFT ⇒ TMP→UNSHIFT(FIFO)
  1. 从临时栈中:
  • TMP→POP ⇒ ARR→UNSHIFT(LOFI)
  • TMP→SHIFT ⇒ ARR→PUSH(FOLI)

其中 ARR 是自引用数组。

英文:

If you want to reverse the array in-place (and return it if you would like), you can create a temporary stack by popping the head of the array until it is empty and then pushing the temporary elements as if they were in a queue.

  1. To temp:
  • ARR→POP ⇒ TMP→PUSH (LILO)
  • ARR→SHIFT ⇒ TMP→UNSHIFT (FIFO)
  1. From temp:
  • TMP→POP ⇒ ARR→UNSHIFT (LOFI)
  • TMP→SHIFT ⇒ ARR→PUSH (FOLI)

Where ARR is the self-reference array.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. if (Array.prototype.reverseItems === undefined) {
  2. Array.prototype.reverseItems = function() {
  3. let tmp = []
  4. while (this.length &gt; 0) tmp.push(this.pop()) // or `tmp.unshift(this.shift()`
  5. while (tmp.length &gt; 0) this.unshift(tmp.pop()) // or `this.push(tmp.shift())`
  6. return this
  7. }
  8. }
  9. let original = [ 9, 0, 3, 4 ]
  10. original.reverseItems() // in-place
  11. console.log(&#39;Reversed:&#39;, original.join(&#39;,&#39;))

<!-- end snippet -->

答案3

得分: -1

@pointy 感谢建议..

我已经修改了这个对象的属性并更新了原始对象

  1. Array.prototype.myReverse = function () {
  2. let arr = this.slice(); // 创建数组的副本
  3. this.splice(0, this.length); // 从数组中删除所有元素
  4. for (let i = 0; i < arr.length; i++) {
  5. this.unshift(arr[i]);
  6. }
  7. return this;
  8. }
  9. let a = [9, 0, 3, 4];
  10. console.log("Before ", a);
  11. console.log("reverse - ", a.myReverse());
  12. console.log("After ", a);

其他用于模拟现有数组原型的链接

https://medium.com/@ofirrifo/naive-implementation-of-js-array-methods-a56319cad6b8

https://gist.github.com/alexhawkins/28aaf610a3e76d8b8264

https://stackoverflow.com/questions/44736780/node-js-change-number-object-value-inside-prototype

英文:

@pointy Thanks for suggestion..

I have modified properties of this and updated original object

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. Array.prototype.myReverse = function () {
  2. let arr = this.slice(); // creating a copy of array
  3. this.splice(0,this.length); // removing all elements from array
  4. for(let i = 0; i&lt;arr.length;i++){
  5. this.unshift(arr[i]);
  6. }
  7. return this;
  8. }
  9. let a = [9,0,3,4];
  10. console.log(&quot;Before &quot;,a);
  11. console.log(&quot;reverse - &quot;, a.myReverse());
  12. console.log(&quot;After &quot;, a);

<!-- end snippet -->

Few other links for native imlimatation of exsiting array prototypes

https://medium.com/@ofirrifo/naive-implementation-of-js-array-methods-a56319cad6b8

https://gist.github.com/alexhawkins/28aaf610a3e76d8b8264

https://stackoverflow.com/questions/44736780/node-js-change-number-object-value-inside-prototype

huangapple
  • 本文由 发表于 2020年1月6日 21:53:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613346.html
  • array.prototype.map
  • arrays
  • javascript
  • prototypal-inheritance
  • prototype-programming
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定