English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Proprietà prototype della stringa JavaScript

 Oggetto String JavaScript

prototypeLe proprietà ti permettono di aggiungere proprietà e metodi ai tuoi oggetti String.

Attenzione:prototype è una proprietà globale disponibile praticamente per tutti gli oggetti (Number, Array, String e Date, ecc.).

Sintassi:

String.prototype.name = value

Crea un nuovo metodo di stringa che restituisce il numero di vocali nel testo fornito:

String.prototype.countVowels = function() {
var x = this.match(/[aeiou]/gi);
return (x === null ? 0 : x.length);
};

Usa nuovi metodi sulle stringhe:

var str = 'Hello world';
str.countVowels(); // return 3

Prova a vedere‹/›

Compatibilità del browser

Tutti i browser supportano completamente la proprietà prototype:

Proprietà
prototype

 Oggetto String JavaScript