英文:
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:
Array.prototype.myReverse = function() {
let length = this.length;
for (let i = 0; i < Math.floor(length / 2); i++) {
let temp = this[i];
this[i] = this[length - 1 - i];
this[length - 1 - i] = temp;
}
return this;
}
let a = [9, 0, 3, 4];
console.log("Before ", a); // [9, 0, 3, 4]
console.log("reverse - ", a.myReverse()); // [4, 3, 0, 9]
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 -->
Array.prototype.myReverse = function() {
let arr = [];
for (let i = 0; i < this.length; i++) {
arr.unshift(this[i]);
}
return arr;
}
let a = [9, 0, 3, 4];
console.log("Before ", a); // [9, 0, 3, 4]
console.log("reverse - ", a.myReverse()); // [4, 3, 0, 9]
//not modifying original object , how to modify original object
console.log("After ", 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
,但你仍然可以更改其属性。因此,保持你发布的代码风格,你可以按照以下方式进行操作:
Array.prototype.myReverse = function() {
let arr = [...this]
for (let i = 0; i < this.length; i++) {
this[i] = arr.pop()
}
}
英文:
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:
Array.prototype.myReverse = function() {
let arr = [...this]
for (let i = 0; i < this.length; i++) {
this[i] = arr.pop()
}
}
答案2
得分: 0
如果你想原地反转数组(并且如果你愿意,返回它),你可以创建一个临时栈,通过弹出数组的头部直到它为空,然后将临时元素推入,就好像它们是在队列中一样。
- 临时栈:
- ARR→POP ⇒ TMP→PUSH(LILO)
- ARR→SHIFT ⇒ TMP→UNSHIFT(FIFO)
- 从临时栈中:
- 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.
- To temp:
- ARR→POP ⇒ TMP→PUSH (LILO)
- ARR→SHIFT ⇒ TMP→UNSHIFT (FIFO)
- 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 -->
if (Array.prototype.reverseItems === undefined) {
Array.prototype.reverseItems = function() {
let tmp = []
while (this.length > 0) tmp.push(this.pop()) // or `tmp.unshift(this.shift()`
while (tmp.length > 0) this.unshift(tmp.pop()) // or `this.push(tmp.shift())`
return this
}
}
let original = [ 9, 0, 3, 4 ]
original.reverseItems() // in-place
console.log('Reversed:', original.join(','))
<!-- end snippet -->
答案3
得分: -1
@pointy 感谢建议..
我已经修改了这个对象的属性并更新了原始对象
Array.prototype.myReverse = function () {
let arr = this.slice(); // 创建数组的副本
this.splice(0, this.length); // 从数组中删除所有元素
for (let i = 0; i < arr.length; i++) {
this.unshift(arr[i]);
}
return this;
}
let a = [9, 0, 3, 4];
console.log("Before ", a);
console.log("reverse - ", a.myReverse());
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 -->
Array.prototype.myReverse = function () {
let arr = this.slice(); // creating a copy of array
this.splice(0,this.length); // removing all elements from array
for(let i = 0; i<arr.length;i++){
this.unshift(arr[i]);
}
return this;
}
let a = [9,0,3,4];
console.log("Before ",a);
console.log("reverse - ", a.myReverse());
console.log("After ", 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论