سؤال

"ahoj".replace("o")
//=> "ahundefinedj"
"ahoj".replace("q")
//=> "ahoj"

Why is that? How is replace implemented? Is there any way I could see the implementation itself?

هل كانت مفيدة؟

المحلول

Pay attention to what you get.

ah undefined j

Spaces added for emphasis. It replaced o with the second argument... which was undefined.

The second replace does nothing because there is no q to replace.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top