Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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中字符串的prototype属性在哪里?_Javascript_String - Fatal编程技术网

JavaScript中字符串的prototype属性在哪里?

JavaScript中字符串的prototype属性在哪里?,javascript,string,Javascript,String,我只是注意到,中的字符串没有原型属性 这是一个教学上的问题,我试着围绕JavaScript类型系统来思考,但给出了什么 “abc.toString()是如何工作的?我该如何扩展字符串呢?如果我希望能够执行“hey you!”。例如,alertDialog()?String.prototype是字符串原型。String.prototype是字符串原型。可以通过引用扩展字符串类 String.prototype.alertDialog = function() { alert(this); };

我只是注意到,中的字符串没有原型属性

这是一个教学上的问题,我试着围绕JavaScript类型系统来思考,但给出了什么


“abc.toString()是如何工作的?我该如何扩展字符串呢?如果我希望能够执行
“hey you!”。例如,alertDialog()

String.prototype是字符串原型。

String.prototype是字符串原型。

可以通过引用扩展字符串类

String.prototype.alertDialog = function() { alert(this); };
String.prototype.yourFunction = function() {}

可以通过引用来扩展String类

String.prototype.yourFunction = function() {}

在处理原型和对象数据类型时,如果使用
for
循环,则完整函数将作为键/值对之一返回。请参阅下面的基本示例和注释

// Basic hash-like Object
var test = {
    'a':1,
    'b':2,
    'c':3,
    'd':4
};

// Incorrect
// badAlerter prototype for Objects
// The last two alerts should show the custom Object prototypes
Object.prototype.badAlerter = function() {
    alert('Starting badAlerter');
    for (var k in this) {
        alert(k +' = '+ this[k]);
    }
};

// Correct
// goodAlerter prototype for Objects
// This will skip functions stuffed into the Object.
Object.prototype.goodAlerter = function() {
    alert('Starting goodAlerter');
    for (var k in this) {
        if (typeof this[k] == 'function') continue;
        alert(k +' = '+ this[k])
    }
};

test.badAlerter();
test.goodAlerter();

在处理原型和对象数据类型时,如果使用
for
循环,则完整函数将作为键/值对之一返回。请参阅下面的基本示例和注释

// Basic hash-like Object
var test = {
    'a':1,
    'b':2,
    'c':3,
    'd':4
};

// Incorrect
// badAlerter prototype for Objects
// The last two alerts should show the custom Object prototypes
Object.prototype.badAlerter = function() {
    alert('Starting badAlerter');
    for (var k in this) {
        alert(k +' = '+ this[k]);
    }
};

// Correct
// goodAlerter prototype for Objects
// This will skip functions stuffed into the Object.
Object.prototype.goodAlerter = function() {
    alert('Starting goodAlerter');
    for (var k in this) {
        if (typeof this[k] == 'function') continue;
        alert(k +' = '+ this[k])
    }
};

test.badAlerter();
test.goodAlerter();

这里的约定是什么?每个对象都应该大写:object、String、Regexp、Number、Date等等……约定是构造函数大写。对象不必大写,也应该大写,以免与构造函数混淆在我的脑海中,我想的是构造函数,而不是对象:)这里的约定是什么?每个对象都应该大写:对象、字符串、Regexp、数字、日期等等……约定是构造函数大写。对象不必是资本化的,而且应该是资本化的,以免它们与构造器混淆在我的脑海中,我在想构造器,非对象:)和
函数中的此
将引用调用方法的字符串。函数中的
将引用调用方法的字符串。谁将对
字符串
执行for循环?您也不应该将
用于带有
数组的
。然而,为了获得最大的兼容性,最好不要修改原生JS对象的原型,后者将在
字符串上执行for循环?您也不应该将
用于带有
数组的
。然而,为了获得最大的兼容性,最好不要修改原生JS对象的原型