简体   繁体   English

javascript,无法覆盖功能

[英]javascript, can not override function

how can I override function length in other file js ? 如何覆盖其他文件js中的函数长度?

  jQuery(function ($) {    

    $.fn.normal_text = function (text) {

           $.fn.normal_text.length = function(){
              ...
           }

        }

}

I try this: 我试试这个:

$(document).ready(function(){
    $.fn.normal_text.length = function(){ alert('123'); }
});

But it not work.I seem to be crazy with it 但它不起作用。我似乎很疯狂

You can't override the length property on functions because it is a read-only builtin. 您不能覆盖函数的length属性,因为它是只读内置函数。

Under "Properties of function instances" in the language specification: 在语言规范中的“函数实例的属性”下:

15.3.5.1 length 15.3.5.1长度

The value of the length property is an integer that indicates the “typical” number of arguments expected by the function. length属性的值是一个整数,表示函数预期的“典型”参数数。 However, the language permits the function to be invoked with some other number of arguments. 但是,该语言允许使用其他数量的参数调用该函数。 The behaviour of a function when invoked on a number of arguments other than the number specified by its length property depends on the function. 在除了由length属性指定的数量之外的多个参数上调用时,函数的行为取决于函数。 This property has the attributes { [[Writable]]: false , [[Enumerable]]: false, [[Configurable]]: false } . 此属性具有属性{ [[Writable]]: false , [[Enumerable]]: false, [[Configurable]]: false }

The bold part explains that the length property on function instances is not writable. 粗体部分解释了函数实例上的length属性不可写。

normal_text是一个函数,而不是一个对象,所以你不能覆盖它的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM