Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用原型更改javascript中的内置方法_Javascript_Function_Object_Methods_Prototype - Fatal编程技术网

使用原型更改javascript中的内置方法

使用原型更改javascript中的内置方法,javascript,function,object,methods,prototype,Javascript,Function,Object,Methods,Prototype,我有三个问题,它们都是相关的 1) 我想将名为bar的属性添加到String对象。 是我干的 String.prototype.bar=function() {console.log("jjjj");} "mystring.bar//function() "mystring".bar()//jjjj >>> String.prototype.length=function(){console.log("xxx");}; function() >>> "mmm

我有三个问题,它们都是相关的

1) 我想将名为
bar
的属性添加到
String
对象。 是我干的

String.prototype.bar=function() {console.log("jjjj");}
"mystring.bar//function()
"mystring".bar()//jjjj
>>> String.prototype.length=function(){console.log("xxx");};
function()
>>> "mmmm".length
4
但是我想使用
“mystring.bar
(无函数调用),就像我们使用
“mystring.length
获取字符串长度一样。我怎样才能做到这一点

2) 现在我想更改String对象的内置长度方法。所以我做了这个

String.prototype.bar=function() {console.log("jjjj");}
"mystring.bar//function()
"mystring".bar()//jjjj
>>> String.prototype.length=function(){console.log("xxx");};
function()
>>> "mmmm".length
4
但长度方法没有改变。它仍然只返回字符串的长度。 注意:我只是为了学习而在控制台上更改此方法

3) 我很难从Crockford的《javascript的好部分》一书中理解这个函数。(第40页) 下面是扩充字符串对象的方法。它替换字符串中的HTML实体,并用它们的等价物替换它们

String.method('deentityify',function() {
var entity =  {
quot:'";,
lt:'<',
gt:'<'
};
//return the deentityify method
return function() {
  return this.replace(/&([^&;]+);/g,
         function (a,b) {
         var r = entity[b];
         return typeof r==='string' ? r : a;
        }
     );
   };
}());
'&lt;&quot;&gt;'.dentityify();//<">
String.method('deentityify',function(){
var实体={
引用:“;”;,
lt:“关于(1)-不要使用
函数
,直接将属性分配给原型:

String.prototype.bar = "bar from string prototype";
alert('some text'.bar); // "bar from string prototype"
关于(2)-我认为这是不可能的

关于(3)Crockford将
方法定义为:

Function.prototype.method = function(name, func) {
   this.prototype[name] = func;
   return this;
};
因此,它是一个定制实现,旨在隐藏关于(1)的
原型的使用-不要使用
函数,直接将属性分配给原型:

String.prototype.bar = "bar from string prototype";
alert('some text'.bar); // "bar from string prototype"
关于(2)-我认为这是不可能的

关于(3)Crockford将
方法定义为:

Function.prototype.method = function(name, func) {
   this.prototype[name] = func;
   return this;
};
因此,它是一个定制实现,旨在隐藏
原型的使用

我想使用“mystring.bar”(无函数调用),就像我们使用“mystring.length”来获取字符串的长度一样。我如何实现这一点

假设您希望使用函数动态计算某个值,但希望使用普通属性访问的语法,而不是函数,则必须创建具有以下内容的getter属性:

但是length方法没有改变。它仍然只返回字符串的长度

那是因为:

创建字符串对象后,此属性将保持不变。它具有以下属性:
{[[Writable]]:false、[[Enumerable]]:false、[[Configurable]]:false}


我不明白为
a
b
[…]提供了哪些参数,以及匿名函数是如何执行的

对于正则表达式找到的每个匹配项,回调在内部由执行。中解释了参数。第一个参数是整个匹配项,后面的每个参数是表达式中一个捕获组的值

我想使用“mystring.bar”(无函数调用),就像我们使用“mystring.length”来获取字符串的长度一样。我如何实现这一点

假设您希望使用函数动态计算某个值,但希望使用普通属性访问的语法,而不是函数,则必须创建具有以下内容的getter属性:

但是length方法没有改变。它仍然只返回字符串的长度

那是因为:

创建字符串对象后,此属性将保持不变。它具有以下属性:
{[[Writable]]:false、[[Enumerable]]:false、[[Configurable]]:false}


我不明白为
a
b
[…]提供了哪些参数,以及匿名函数是如何执行的

对于正则表达式找到的每个匹配项,回调在内部由执行。中解释了参数。第一个参数是整个匹配项,后面的每个参数是表达式中一个捕获组的值