本文目錄一覽:
- 1、js判斷輸入是否為數字的具體實例
- 2、js判斷對象的屬性是原型的還是實例的
- 3、js判斷類型instanceof 怎麼用
- 4、JavaScript比較兩個對象是否相等幾個例子
- 5、JS 判斷一個值是不是為數字
- 6、JS怎麼判斷一個對象是否為空
js判斷輸入是否為數字的具體實例
titlejs判斷輸入是否為數字/title
script language=”javascript教程”
function ischeckNum(){var num = document.getElementById(‘isnum’).value;if( num ){if( !isNaN( num ) ){alert(‘是數字’);
return false;}else{alert(‘你輸入的數據不是數字’);
myfm.isnum.select();
return false;}}else{alert(‘需輸入內容’);
myfm.isnum.focus();}}/script/headbodyform name=”myfm” method=”post” action=””labelinput type=”text” name=”isnum” id=”isnum”/labellabelinput type=”button” name=”Submit” value=”檢測是否為數字” onClick=”ischeckNum();”/label/form/body/html 註明:在javascript中要判斷用戶輸入的內容是否為數字我們只要用isNaN來判斷一下就OK了。
isNaN(numValue) 其中必選項 numvalue 參數為要檢查是否為 NAN 的值
如果值是 NaN, 那麼 isNaN 函數返回 true ,否則返回 false
使用這個函數的典型情況是檢查 parseInt 和 parseFloat 方法的返回值。還有一種辦法,變量可以與它自身進行比較。 如果比較的結果不等,那麼它就是 NaN 。
js判斷對象的屬性是原型的還是實例的
1.hasOwnProperty()函數用於指示一個對象自身(不包括原型鏈)是否具有指定名稱的屬性。如果有,返回true,否則返回false。
2.(屬性名稱 in 對象) 不管屬性是原型的還是實例的,只要存在就返回ture否則返回false
那麼我們可以利用這兩個方法做一個對比,如果實例中沒有且存在了這個屬性,那麼就是原型的
js判斷類型instanceof 怎麼用
在 JavaScript 中,判斷一個變量的類型嘗嘗會用 typeof 運算符,在使用 typeof 運算符時採用引用類型存儲值會出現一個問題,無論引用的是什麼類型的對象,它都返回 “object”。這就需要用到instanceof來檢測某個對象是不是另一個對象的實例。
另外,更重的一點是 instanceof 可以在繼承關係中用來判斷一個實例是否屬於它的父類型。
例如:
function Foo(){}
Foo.prototype = new Aoo();//JavaScript 原型繼承
var foo = new Foo();
console.log(foo instanceof Foo)//true
console.log(foo instanceof Aoo)//true
上面的代碼中是判斷了一層繼承關係中的父類,在多層繼承關係中,instanceof 運算符同樣適用。
又如:
console.log(Object instanceof Object);//true
console.log(Function instanceof Function);//true console.log(Number instanceof Number);//false
console.log(String instanceof String);//false
console.log(Function instanceof Object);//true
console.log(Foo instanceof Function);//true
console.log(Foo instanceof Foo);//false
JavaScript比較兩個對象是否相等幾個例子
本js代碼通過對js對象進行各方面的比較來判斷兩個對象是否相等
cmp = function( x, y ) {
// If both x and y are null or undefined and exactly the same
if ( x === y ) {
return true;
}
// If they are not strictly equal, they both need to be Objects
if ( ! ( x instanceof Object ) || ! ( y instanceof Object ) ) {
return false;
}
//They must have the exact same prototype chain,the closest we can do is
//test the constructor.
if ( x.constructor !== y.constructor ) {
return false;
}
for ( var p in x ) {
//Inherited properties were tested using x.constructor === y.constructor
if ( x.hasOwnProperty( p ) ) {
// Allows comparing x[ p ] and y[ p ] when set to undefined
if ( ! y.hasOwnProperty( p ) ) {
return false;
}
// If they have the same strict value or identity then they are equal
if ( x[ p ] === y[ p ] ) {
continue;
}
// Numbers, Strings, Functions, Booleans must be strictly equal
if ( typeof( x[ p ] ) !== “object” ) {
return false;
}
// Objects and Arrays must be tested recursively
if ( ! Object.equals( x[ p ], y[ p ] ) ) {
return false;
}
}
}
for ( p in y ) {
// allows x[ p ] to be set to undefined
if ( y.hasOwnProperty( p ) ! x.hasOwnProperty( p ) ) {
return false;
}
}
return true;
};
使用:
objA={
a:’123′,
b:’456′
};
objB={
a:’123′,
b:’000′
};
var isEqual= cmp(objA, objB);
console.log(isEqual); // false 不相同
JS 判斷一個值是不是為數字
js判斷是否是數字
第一種方法 isNaN
isNaN返回一個 Boolean 值,指明提供的值是否是保留值 NaN (不是數字)。
NaN 即 Not a Number
isNaN(numValue)
但是如果numValue果是一個空串或是一個空格,而isNaN是做為數字0進行處理的,而parseInt與parseFloat是返回一個錯誤消息,這個isNaN檢查不嚴密而導致的。
第二種方法 正則表達式
function checkRate(input) {
var re = /^[0-9]+.?[0-9]*$/; //判斷字符串是否為數字 //判斷正整數 /^[1-9]+[0-9]*]*$/
var nubmer = document.getElementById(input).value;
if (!re.test(nubmer)) {
alert(“請輸入數字”);
document.getElementById(input).value = “”;
return false;
}
}
第三種方法 利用parseFloat的返回值
/*—-0313————-驗證數據 是數字:返回true;不是數字:返回false——–工具方法,不含有業務邏輯———————*/
function isNotANumber(inputData) {
//isNaN(inputData)不能判斷空串或一個空格
//如果是一個空串或是一個空格,而isNaN是做為數字0進行處理的,而parseInt與parseFloat是返回一個錯誤消息,這個isNaN檢查不嚴密而導致的。
if (parseFloat(inputData).toString() == “NaN”) {
//alert(“請輸入數字……”);注掉,放到調用時,由調用者彈出提示。
return false;
} else {
return true;
}
}
JS怎麼判斷一個對象是否為空
判斷一個對象是否為空,介紹如下三種判斷方法:
1、直接用for…in…遍歷屬性,結果為真是“非空數組”,否則是“空數組”,代碼如下:
function judgeObj(obj){
for(var a in obj){
return alert(‘非空對象’)
}
return alert(‘空對象’)
}
2、通過JSON自帶的.stringify方法來判斷,代碼如下:
if(JSON.stringify(c)=='{}’){
console.log(‘空對象’);
}
3、ES6新增的方法Object.keys(),代碼如下:
if(Object.keys(obj).length==0){
console.log(‘空對象’);
}else{
console.log(‘非空對象’);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/233883.html