JSFiddle - React, Tailwind, and code Playground
by alfredbing
HTML
Javascript擴充方法
JavaScript
//String擴充方法 - Contains
String.prototype.Contains= function(str){
return (this.indexOf(str) != -1);
}
String.prototype.IsFilled = function () {
if (this && this.length > 0) {
return true;
}
return false;
}
String.prototype.IsEmpty = function () {
return !this.IsFilled();
}
//function擴充方法 - getData
sample.prototype.getData = function() {
return this.i;
}
//==============================================
//String使用擴充方法
var name="Loek van den Ouweland";
alert("foobar".Contains("foo")); //alerts true
alert(name.IsFilled()); //alerts true
alert(name.IsEmpty()); //alerts false
//Function使用擴充方法
function sample(i) {
this.i = i;
}
var s = new sample(42);
alert(s.getData()); //alerts 42