当字符串不可变时,如何使用它的原型向字符串添加方法?
How can I add a method to a string, using it's prototype when it is immutable?
String 是不可变的,这意味着我们只能读取它的 属性,不能修改或创建或删除任何 属性 或方法。所以基本上字符串是冻结的。
我的问题是如何向字符串原型添加新方法?
var a = '';
String.prototype.hi = function(){
console.log('hi');
}
a.hi()
Output: hi
为什么没有抛出任何错误?
是的,可以像对其他对象一样向 String 原型添加新函数。
尽管 JavaScript|MDN String 上的这些信息值得一看
只需从您的代码中删除调用即可。
代码:
var a = '';
String.prototype.hi = function(){
console.log('hi');
}
a.hi();
String is immutable which means we can only read its property, not modify or create or delete any of the property or method
这不是真的。字符串是不可变的,我们的意思是字符串对象本身是冻结的。这并不意味着 String.prototype
,它是一个 单独的对象 ,被冻结。当我们向 String.prototype
对象 添加属性时,这并不意味着我们已经改变了任何字符串。它只是 另一个 对象而不是字符串本身。
String
in Javascript 是不可变的,是真命题。所以,你是对的。
因此,字符串的值是不可变的,但不是 prototype
。 String 从其父对象继承了 prototype
。原型中可用的函数不修改字符串的值,它 returns 一个新的字符串实例。
When it comes to inheritance, JavaScript only has one construct:
objects. Each object has a private property which holds a link to
another object called its prototype. That prototype object has a
prototype of its own, and so on until an object is reached with null
as its prototype. By definition, null has no prototype, and acts as
the final link in this prototype chain.
javascript中字符串的值与原型松耦合。如果将字符串的原型替换为空对象,则值不会发生变化。当您访问它时,您将获得与该字符串完全相同的值。
关于原型,你已经有足够的答案是什么了......我想你甚至知道它。
所以在你的例子中你没有改变它自己的字符串,你注意到的是 primitive wrapper objects
示例:
var a = 'A';
String.prototype.test = function() {
return this;
}
console.log('a primitive type =>', typeof a)
console.log('a primitive object wrapper type =>', typeof a.test());
String 是不可变的,这意味着我们只能读取它的 属性,不能修改或创建或删除任何 属性 或方法。所以基本上字符串是冻结的。
我的问题是如何向字符串原型添加新方法?
var a = '';
String.prototype.hi = function(){
console.log('hi');
}
a.hi()
Output: hi
为什么没有抛出任何错误?
是的,可以像对其他对象一样向 String 原型添加新函数。
尽管 JavaScript|MDN String 上的这些信息值得一看 只需从您的代码中删除调用即可。
代码:
var a = '';
String.prototype.hi = function(){
console.log('hi');
}
a.hi();
String is immutable which means we can only read its property, not modify or create or delete any of the property or method
这不是真的。字符串是不可变的,我们的意思是字符串对象本身是冻结的。这并不意味着 String.prototype
,它是一个 单独的对象 ,被冻结。当我们向 String.prototype
对象 添加属性时,这并不意味着我们已经改变了任何字符串。它只是 另一个 对象而不是字符串本身。
String
in Javascript 是不可变的,是真命题。所以,你是对的。
因此,字符串的值是不可变的,但不是 prototype
。 String 从其父对象继承了 prototype
。原型中可用的函数不修改字符串的值,它 returns 一个新的字符串实例。
When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.
javascript中字符串的值与原型松耦合。如果将字符串的原型替换为空对象,则值不会发生变化。当您访问它时,您将获得与该字符串完全相同的值。
关于原型,你已经有足够的答案是什么了......我想你甚至知道它。
所以在你的例子中你没有改变它自己的字符串,你注意到的是 primitive wrapper objects
示例:
var a = 'A';
String.prototype.test = function() {
return this;
}
console.log('a primitive type =>', typeof a)
console.log('a primitive object wrapper type =>', typeof a.test());