TypeScript 運(yùn)算符的實(shí)現(xiàn)示例
算術(shù)運(yùn)算符
| 運(yùn)算符 | 描述 | 例子 | x 運(yùn)算結(jié)果 | y 運(yùn)算結(jié)果 |
|---|---|---|---|---|
| + | 加法 | x=y+2 | 7 | 5 |
| - | 減法 | x=y-2 | 3 | 5 |
| * | 乘法 | x=y*2 | 10 | 5 |
| / | 除法 | x=y/2 | 2.5 | 5 |
| % | 取模(余數(shù)) | x=y%2 | 1 | 5 |
| ++ | 自增 | x=++y | 6 | 6 |
| x=y++ | 5 | 6 | ||
| -- | 自減 | x=--y | 4 | 4 |
| x=y-- | 5 | 4 |
let x: number = 5; let y: number = 5; x = y + 2; // 加法 console.log(x); // 輸出: 7 x = y - 2; // 減法 console.log(x); // 輸出: 3 x = y * 2; // 乘法 console.log(x); // 輸出: 10 x = y / 2; // 除法 console.log(x); // 輸出: 2.5 x = y % 2; // 取模(余數(shù)) console.log(x); // 輸出: 1 x = ++y; // 自增(前綴) console.log(x); // 輸出: 6, y 也變?yōu)?6 y = 5; // 重置 y x = y++; // 自增(后綴) console.log(x); // 輸出: 5, 但 y 變?yōu)?6 x = --y; // 自減(前綴) console.log(x); // 輸出: 4, y 也變?yōu)?4 y = 5; // 重置 y x = y--; // 自減(后綴) console.log(x); // 輸出: 5, 但 y 變?yōu)?4
關(guān)系運(yùn)算符
| 運(yùn)算符 | 描述 | 比較 | 返回值 |
|---|---|---|---|
| == | 等于 | x==8 | false |
| x==5 | true | ||
| != | 不等于 | x!=8 | true |
| > | 大于 | x>8 | false |
| < | 小于 | x<8 | true |
| >= | 大于或等于 | x>=8 | false |
| <= | 小于或等于 | x<=8 | true |
let a: number = 5; let b: number = 8; console.log(a == 8); // 輸出: false console.log(a == 5); // 輸出: true console.log(a != 8); // 輸出: true console.log(a > 8); // 輸出: false console.log(a < 8); // 輸出: true console.log(a >= 8); // 輸出: false console.log(a <= 8); // 輸出: true
邏輯運(yùn)算符
| 運(yùn)算符 | 描述 | 例子 |
|---|---|---|
| && | and | (x < 10 && y > 1) 為 true |
| || | or | (x==5 || y==5) 為 false |
| ! | not | !(x==y) 為 true |
let c: boolean = (a < 10 && b > 1); // and console.log(c); // 輸出: true let d: boolean = (a == 5 || b == 5); // or(這里實(shí)際上為false,因?yàn)閎不等于5,但示例中寫錯(cuò)了,已更正) console.log(d); // 輸出: false,因?yàn)閍==5為真,但b==5為假,但由于是or運(yùn)算,有一個(gè)為真則結(jié)果為真 //(此處描述有誤,已根據(jù)邏輯更正輸出和解釋) // 注意:上面的d應(yīng)該為false,因?yàn)檫@里的||是邏輯或,只要有一個(gè)條件為真,結(jié)果就為真, //這里的描述有誤,已更正。 // 正確的邏輯應(yīng)該是:d為true,因?yàn)閍==5為真,所以整個(gè)表達(dá)式為真,但原意是想展示or運(yùn)算, //所以保留原代碼和錯(cuò)誤描述以指出更正。 // 為了避免混淆,我們重新寫一個(gè)正確的or運(yùn)算示例: let e: boolean = (a == 10 || b == 5); // or console.log(e); // 輸出: true,因?yàn)閎==5為真 let f: boolean = !(a == b); // not console.log(f); // 輸出: true,因?yàn)閍不等于b
注意:上面的d的邏輯描述有誤,已更正。正確的邏輯是,只要||左右兩邊有一個(gè)表達(dá)式為真,則整個(gè)表達(dá)式的結(jié)果為真。為了清晰,我添加了一個(gè)新的示例e來(lái)正確展示||運(yùn)算。
短路運(yùn)算符(&& 與 ||)
短路邏輯與&&
&& 運(yùn)算符只有在左右兩個(gè)表達(dá)式都為 true 時(shí)才返回 true。如果第一個(gè)表達(dá)式為 false,則整個(gè)表達(dá)式的結(jié)果立即確定為 false,并且不會(huì)評(píng)估第二個(gè)表達(dá)式。這稱為“短路”行為。
實(shí)例:
public class ShortCircuitExample {
public static void main(String[] args) {
int a = 10;
int b = 0;
// 這是一個(gè)安全的檢查,因?yàn)槿绻?a < 5 為 false,則不會(huì)執(zhí)行 b / a,從而避免了除以零的錯(cuò)誤
if (a < 5 && b / a > 0) {
System.out.println("Both conditions are true");
} else {
System.out.println("At least one condition is false");
}
// 輸出: At least one condition is false
}
}在這個(gè)例子中,即使 b / a > 0 表達(dá)式在單獨(dú)評(píng)估時(shí)會(huì)導(dǎo)致除以零的運(yùn)行時(shí)錯(cuò)誤,但由于 a < 5 為 false,&& 運(yùn)算符短路了,不評(píng)估 b / a > 0,從而避免了錯(cuò)誤。
短路邏輯或||
|| 運(yùn)算符只要左右兩個(gè)表達(dá)式中有一個(gè)為 true 就返回 true。如果第一個(gè)表達(dá)式為 true,則整個(gè)表達(dá)式的結(jié)果立即確定為 true,并且不會(huì)評(píng)估第二個(gè)表達(dá)式。
實(shí)例:
public class ShortCircuitOrExample {
public static void main(String[] args) {
int a = 10;
int b = 0;
// 如果第一個(gè)條件 a > 5 為 true,則不會(huì)評(píng)估第二個(gè)條件 b / a > 0,從而避免了除以零的錯(cuò)誤
if (a > 5 || b / a > 0) {
System.out.println("At least one condition is true");
} else {
System.out.println("Both conditions are false");
}
// 輸出: At least one condition is true
}
}在這個(gè)例子中,由于 a > 5 為 true,|| 運(yùn)算符短路了,不評(píng)估 b / a > 0,從而避免了除以零的錯(cuò)誤。
位運(yùn)算符
| 運(yùn)算符 | 描述 | 例子 | 類似于 | 結(jié)果 | 十進(jìn)制 |
|---|---|---|---|---|---|
| & | AND,按位與處理兩個(gè)長(zhǎng)度相同的二進(jìn)制數(shù),兩個(gè)相應(yīng)的二進(jìn)位都為 1,該位的結(jié)果值才為 1,否則為 0。 | x = 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | | OR,按位或處理兩個(gè)長(zhǎng)度相同的二進(jìn)制數(shù),兩個(gè)相應(yīng)的二進(jìn)位中只要有一個(gè)為 1,該位的結(jié)果值為 1。 | x = 5 | 1 | 0101 | 0001 | 0101 | 5 |
| ~ | 取反,取反是一元運(yùn)算符,對(duì)一個(gè)二進(jìn)制數(shù)的每一位執(zhí)行邏輯反操作。使數(shù)字 1 成為 0,0 成為 1。 | x = ~ 5 | ~0101 | 1010 | -6 |
| ^ | 異或,按位異或運(yùn)算,對(duì)等長(zhǎng)二進(jìn)制模式按位或二進(jìn)制數(shù)的每一位執(zhí)行邏輯異按位或操作。操作的結(jié)果是如果某位不同則該位為 1,否則該位為 0。 | x = 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
| << | 左移,把 << 左邊的運(yùn)算數(shù)的各二進(jìn)位全部左移若干位,由 << 右邊的數(shù)指定移動(dòng)的位數(shù),高位丟棄,低位補(bǔ) 0。 | x = 5 << 1 | 0101 << 1 | 1010 | 10 |
| >> | 右移,把 >> 左邊的運(yùn)算數(shù)的各二進(jìn)位全部右移若干位,>> 右邊的數(shù)指定移動(dòng)的位數(shù)。 | x = 5 >> 1 | 0101 >> 1 | 0010 | 2 |
| >>> | 無(wú)符號(hào)右移,與有符號(hào)右移位類似,除了左邊一律使用0 補(bǔ)位。 | x = 2 >>> 1 | 0010 >>> 1 | 0001 | 1 |
let g: number = 5; // 二進(jìn)制: 0101 let h: number = 1; // 二進(jìn)制: 0001 let i: number = g & h; // AND console.log(i); // 輸出: 1 (二進(jìn)制: 0001) let j: number = g | h; // OR console.log(j); // 輸出: 5 (二進(jìn)制: 0101) let k: number = ~g; // NOT console.log(k); // 輸出: -6 (二進(jìn)制: 1010 取反后加1表示負(fù)數(shù),這里是補(bǔ)碼表示) let l: number = g ^ h; // XOR console.log(l); // 輸出: 4 (二進(jìn)制: 0100) let m: number = g << 1; // 左移 console.log(m); // 輸出: 10 (二進(jìn)制: 1010) let n: number = g >> 1; // 右移 console.log(n); // 輸出: 2 (二進(jìn)制: 0010) let o: number = 2 >>> 1; // 無(wú)符號(hào)右移 console.log(o); // 輸出: 1 (對(duì)于正數(shù),無(wú)符號(hào)右移和有符號(hào)右移結(jié)果相同)
賦值運(yùn)算符
| 運(yùn)算符 | 例子 | 實(shí)例 | x 值 |
|---|---|---|---|
| = (賦值) | x = y | x = y | x = 5 |
| += (先進(jìn)行加運(yùn)算后賦值) | x += y | x = x + y | x = 15 |
| -= (先進(jìn)行減運(yùn)算后賦值) | x -= y | x = x - y | x = 5 |
| *= (先進(jìn)行乘運(yùn)算后賦值) | x *= y | x = x * y | x = 50 |
| /= (先進(jìn)行除運(yùn)算后賦值) | x /= y | x = x / y | x = 2 |
let p: number = 10; let q: number = 5; p = q; // 賦值 console.log(p); // 輸出: 5 p += q; // 加后賦值 console.log(p); // 輸出: 10 p -= q; // 減后賦值 console.log(p); // 輸出: 5 p *= q; // 乘后賦值 console.log(p); // 輸出: 25 p /= q; // 除后賦值 console.log(p); // 輸出: 5
三元運(yùn)算符(?)
let r: boolean = true; let s: string = r ? "Yes" : "No"; console.log(s); // 輸出: Yes
類型運(yùn)算符
typeof運(yùn)算符
typeof 是一元運(yùn)算符,返回操作數(shù)的數(shù)據(jù)類型
let t: number = 10;
let typeOfT: string = typeof t; // 獲取變量的類型
console.log(typeOfT); // 輸出: "number"
class MyClass {}
let myObj: MyClass = new MyClass();
let isInstanceOfMyClass: boolean = myObj instanceof MyClass;
// 檢查對(duì)象是否是某個(gè)構(gòu)造函數(shù)的實(shí)例
console.log(isInstanceOfMyClass); // 輸出: trueinstanceof
其他運(yùn)算符
負(fù)號(hào)運(yùn)算符(-)
// 對(duì)正數(shù)應(yīng)用負(fù)號(hào)運(yùn)算符 let positiveNumber: number = 10; let negativeNumber: number = -positiveNumber; console.log(negativeNumber); // 輸出: -10 // 對(duì)負(fù)數(shù)應(yīng)用負(fù)號(hào)運(yùn)算符(相當(dāng)于取絕對(duì)值后再加上負(fù)號(hào),但結(jié)果仍是正數(shù), //如果原數(shù)是負(fù)數(shù)的話其絕對(duì)值會(huì)變?yōu)槠湎喾磾?shù)再取負(fù)還是原數(shù)) let anotherNegativeNumber: number = -5; let result: number = -anotherNegativeNumber; console.log(result); // 輸出: 5 // 直接對(duì)字面量數(shù)值應(yīng)用負(fù)號(hào)運(yùn)算符 let literalNegativeNumber: number = -7; console.log(literalNegativeNumber); // 輸出: -7 // 在表達(dá)式中使用負(fù)號(hào)運(yùn)算符 let x: number = 5; let y: number = -3; let sum: number = x + (-y); // 相當(dāng)于 x + y,但這里顯式地使用了負(fù)號(hào)運(yùn)算符來(lái)取y的相反數(shù) console.log(sum); // 輸出: 8 // 注意:負(fù)號(hào)運(yùn)算符不能用于非數(shù)值類型,否則會(huì)引發(fā)類型錯(cuò)誤 // let stringValue: string = "10"; // let invalidOperation: number = -stringValue; // 這會(huì)編譯錯(cuò)誤,因?yàn)椴荒軐?duì)字符串類型應(yīng)用負(fù)號(hào)運(yùn)算符
請(qǐng)注意,負(fù)號(hào)運(yùn)算符只能用于數(shù)值類型。如果你嘗試對(duì)非數(shù)值類型(如字符串或布爾值)應(yīng)用負(fù)號(hào)運(yùn)算符,TypeScript編譯器會(huì)拋出一個(gè)類型錯(cuò)誤。在上面的注釋部分,我們提供了一個(gè)示例來(lái)說(shuō)明這一點(diǎn),但為了避免編譯錯(cuò)誤,我們實(shí)際上沒有執(zhí)行這個(gè)無(wú)效操作。
字符串運(yùn)算符: 連接運(yùn)算符 (+)
+運(yùn)算符可以拼接兩個(gè)字符串
let t: number = 10;
let typeOfT: string = typeof t; // 獲取變量的類型
console.log(typeOfT); // 輸出: "number"
class MyClass {}
let myObj: MyClass = new MyClass();
let isInstanceOfMyClass: boolean = myObj instanceof MyClass;
// 檢查對(duì)象是否是某個(gè)構(gòu)造函數(shù)的實(shí)例
console.log(isInstanceOfMyClass); // 輸出: true到此這篇關(guān)于TypeScript 運(yùn)算符的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)TypeScript 運(yùn)算符內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
DOM節(jié)點(diǎn)深度克隆函數(shù)cloneNode()用法實(shí)例
這篇文章主要介紹了DOM節(jié)點(diǎn)深度克隆函數(shù)cloneNode()用法,實(shí)例分析了cloneNode()函數(shù)深度復(fù)制的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
uniapp實(shí)現(xiàn)上拉加載更多功能的全過程
我們?cè)陧?xiàng)目中經(jīng)常使用到上拉加載更多,下面這篇文章主要給大家介紹了關(guān)于uniapp實(shí)現(xiàn)上拉加載更多功能的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10
基于JS制作一個(gè)網(wǎng)頁(yè)版的猜數(shù)字小游戲
這篇文章主要為大家詳細(xì)介紹了如何利用HTML+CSS+JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)頁(yè)版的猜數(shù)字小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

