我如何得到“this = this”在原型工作 [英] how do i get "this = this" in prototype working
问题描述
好看,所以我知道弄乱原型是不好的做法,但无论如何......
Ok peep's so I know it's bad practice to mess with prototypes but here it is anyway...
Array.prototype.rev=
function(){
this.reverse();
}
工作正常!按预期更新源数组变量 ary
,例如:
Works fine! Updates the source array variable, ary
, as expected eg:
ary = [123, 456];
ary.rev();
// result: ary == [456, 123]
为 String
写一个类似的属性时出现问题。
My problem comes when writing a similar property for String
.
什么我想做的就是这样...
What I would like to do is something like this...
String.prototype.rev=
function(){
this.split('');
this.reverse();
this.join('');
}
看起来很简单吧!拆分字符串,反转它,然后将它连接在一起,这样原来的字符串变量 str
就像它以前的自身一样,与 ary
以上!
Seems simple enough right! Split the string, reverse it, and join it back together so the original string variable, str
, is a reverse of it's former self, as was with the ary
above!
事情是:虽然 this.split()
已经调用它需要存储为变量,即:
Thing is: Although this.split()
has been called it needs to be stored as a variable, ie:
split = this.split('');
还有 this = this
问题...
现在拆分
已定义,它将注意力从编辑源变量上移开,并不像我在函数结尾处所说的那样:
Now split
is defined, it takes the focus away from editing the source variable and it's not like I can just say at the end of the function:
this = split;
作为这个
是'不可变'还是什么它们是否意味着它是静态的且不可改变的?
As this
is 'immutable' or what ever it is when they mean it is static and unchangeable?
str = 'abc'
我希望能够说 str.rev()
而不是 str = str .rev()
并获得 str ='cba'的结果
其中 str ==='cba'
,抓住我的漂移?!
I want to be able to say str.rev()
not str = str.rev()
and get the result of str = 'cba'
where str === 'cba'
, catch my drift?!
所有的解决方法和学费欢迎窥视,我只是问你知道你在说什么回合。 thx
All work-around's and tuition welcome peep's, I just ask that u know what ur talkin' 'bout. thx
推荐答案
为
String <编写类似属性时出现问题/ code> ...
My problem comes when writing a similar property for
String
...
这里的主要问题是JavaScript中的字符串不可变 ;你不能改变一个字符串。因此,不可能定义一个行为如下的 rev
方法:
The main issue here is that strings are immutable in JavaScript; you cannot change a string in place. Because of that, it's impossible to define a rev
method that would behave like this:
var a = 'abc';
a.rev(); // <== This can't work this way
console.log(a); // cba
相反,你的 rev
应该做所有其他 String
方法:返回带有更新的新字符串。
Instead, your rev
should do what all other String
methods do: Return a new string with the updates.
第二个问题是您的 rev
方法中的代码不起作用,因为您没有保存 this.split等内容的结果('');
在任何地方,但 split
的方式有效,返回带有条目的数组。
A secondary issue is that your code in your rev
method doesn't work, because you're not saving the results of things like this.split('');
anywhere, but the way split
works, it returns an array with the entries.
这是一个解决这两个问题的 rev
版本:
Here's a version of rev
that addresses both issues:
String.prototype.rev=
function(){
return this.split('').reverse().join('');
};
然后:
var a = 'abc'.rev();
console.log(a); // cba
示例:
String.prototype.rev = function(){
return this.split('').reverse().join('');
};
var a = 'abc'.rev();
snippet.log(a);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
这是所有标准字符串方法( toLowerCase
,替换
, substring $)的方式c $ c>,...)工作,返回结果。
This is how all of the standard string methods (toLowerCase
, replace
, substring
, ...) work, by returning the result.
所有在线版本可能不太清晰(并且很难调试),这里是拆分版本清晰度:
The all-on-one-line version might not be hyper clear (and is hard to debug), here's the split-apart version for clarity:
String.prototype.rev=
function(){
var characters = this.split('');
characters.reverse();
return characters.join('');
};
(注意 Array#reverse
撤销就地放置和返回数组引用;它还返回数组引用的事实是使一体化版本成为可能的原因。)
(Note that Array#reverse
reverses the array in place and returns the array reference; the fact it also returns the array reference is what makes the all-in-one-line version possible.)
附注:如果您打算使用原型,请考虑使用 Object.defineProperty
而不是仅仅分配:
Side note: If you're going to play around with prototypes, consider using Object.defineProperty
rather than just assigning:
Object.defineProperty(String.prototype, "rev", {
value: function() { ... }
});
...以便新属性不可枚举(不显示在 for-in
循环)。使用 String
并不重要,但很多人仍然错误地使用 for-in
用于循环遍历数组,所以......
...so that the new properties are non-enumerable (don't show up in for-in
loops). Doesn't matter that much with String
, but a lot of people still incorrectly use for-in
for looping through arrays, so...
示例:
Object.defineProperty(String.prototype, "rev", {
value: function(){
return this.split('').reverse().join('');
}
});
var a = 'abc'.rev();
snippet.log(a);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
这篇关于我如何得到“this = this”在原型工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!