JavaScript中this取值深度解讀
前言:被 this 折磨的前端日常
“為什么函數(shù)里的 this 一會(huì)兒是 window,一會(huì)兒是 undefined?”
“對(duì)象方法里的 this,賦值給變量后調(diào)用怎么就指向全局了?”
“箭頭函數(shù)的 this 為什么跟外層函數(shù)一模一樣,改都改不了?”
“用 new 創(chuàng)建實(shí)例時(shí),this 明明指向?qū)嵗?,怎么返回個(gè)對(duì)象就變了?”
this 是 JavaScript 中最容易讓人困惑的概念之一 —— 它既不是 “定義時(shí)綁定”,也不是 “誰(shuí)調(diào)用就指向誰(shuí)” 這么簡(jiǎn)單。很多開(kāi)發(fā)者靠 “經(jīng)驗(yàn)猜 this”,遇到復(fù)雜場(chǎng)景就陷入調(diào)試?yán)Ь?。本文將?“執(zhí)行上下文本質(zhì)” 出發(fā),通過(guò) “場(chǎng)景復(fù)現(xiàn)→原理拆解→代碼驗(yàn)證→避坑指南” 的邏輯,幫你徹底搞懂 this 的取值規(guī)則,從此不再靠 “猜” 寫代碼。
一、先破后立:this 的本質(zhì)不是 “誰(shuí)調(diào)用指向誰(shuí)”
在拆解具體場(chǎng)景前,必須先糾正一個(gè)流傳甚廣的誤區(qū):this 不是 “誰(shuí)調(diào)用指向誰(shuí)”,而是 “函數(shù)調(diào)用時(shí),執(zhí)行上下文綁定的一個(gè)變量”。
1.1 this 的核心特性:執(zhí)行時(shí)綁定
this 的指向在函數(shù)定義時(shí)完全不確定,只有在函數(shù)被調(diào)用的那一刻,才會(huì)根據(jù) “調(diào)用方式” 綁定到具體對(duì)象,也就是無(wú)論這個(gè)函數(shù)聲明在哪里、被賦值過(guò)多少次。這是理解 this 的第一個(gè)關(guān)鍵:
// 函數(shù)定義時(shí),this毫無(wú)意義
function sayHi() {
console.log(this.name);
}
// 調(diào)用方式1:普通函數(shù)調(diào)用 → this指向window(瀏覽器)/global(Node)
const name = "全局";
sayHi(); // 輸出“全局”
// 調(diào)用方式2:對(duì)象方法調(diào)用 → this指向?qū)ο?
const obj = { name: "張三", sayHi };
obj.sayHi(); // 輸出“張三”
// 調(diào)用方式3:new調(diào)用 → this指向新實(shí)例
function Person(name) {
this.name = name;
}
const p = new Person("李四");
console.log(p.name); // 輸出“李四”
同樣的函數(shù)sayHi,只因調(diào)用方式不同,this 指向完全不同 —— 這說(shuō)明 “調(diào)用方式” 才是 this 綁定的核心依據(jù)。
1.2 this 的底層邏輯:執(zhí)行上下文
JavaScript 執(zhí)行函數(shù)時(shí),會(huì)創(chuàng)建一個(gè) “執(zhí)行上下文(Execution Context)”,其中包含三個(gè)核心變量:
this:當(dāng)前函數(shù)的調(diào)用者關(guān)聯(lián)對(duì)象
AO(Activation Object):函數(shù)的活動(dòng)對(duì)象(存儲(chǔ)局部變量、參數(shù)等)
作用域鏈:決定變量的查找范圍
this 是執(zhí)行上下文的固有屬性,其值由 “函數(shù)調(diào)用時(shí)的調(diào)用點(diǎn)(Call Site)” 決定,而非函數(shù)定義的位置。
1.3 this 綁定規(guī)則的優(yōu)先級(jí)
當(dāng)多個(gè)規(guī)則同時(shí)生效時(shí),優(yōu)先級(jí)決定最終 this 指向,優(yōu)先級(jí)從高到低:new 綁定 > 顯式綁定(bind) > 隱式綁定 > 默認(rèn)綁定
驗(yàn)證優(yōu)先級(jí):
- 顯式綁定 > 隱式綁定:
const obj1 = { a: 1, foo: function() { console.log(this.a); } };
const obj2 = { a: 2 };
obj1.foo.call(obj2); // 輸出 2 → 顯式綁定覆蓋隱式
- new 綁定 > bind 顯式綁定:
function foo(a) { this.a = a; }
const boundFoo = foo.bind({ a: 10 }); // 顯式綁定到 {a:10}
const instance = new boundFoo(20); // new 綁定
console.log(instance.a); // 輸出 20 → new 覆蓋 bind
附上bind的實(shí)現(xiàn)代碼(可知:new 綁定 > bind 顯式綁定):
Function.prototype.bind = function (context) {
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
var fBound = function () {
var bindArgs = Array.prototype.slice.call(arguments);
// new創(chuàng)建的時(shí)候,this指向new出來(lái)的對(duì)象實(shí)例,這個(gè)實(shí)例通過(guò)new創(chuàng)建的,那么實(shí)例的constructor指向fBound
return self.apply(this instanceof fBound ? this : context, args.concat(bindArgs));
}
// 修改返回函數(shù)的 prototype 為綁定函數(shù)的 prototype,實(shí)例就可以繼承綁定函數(shù)的原型中的值
fBound.prototype = this.prototype;
return fBound;
}
二、場(chǎng)景拆解:6 種核心調(diào)用方式的 this 綁定規(guī)則
實(shí)際開(kāi)發(fā)中,this 的綁定場(chǎng)景可歸納為 6 類,覆蓋 99% 的業(yè)務(wù)需求。每類場(chǎng)景都有明確的綁定規(guī)則,掌握這些規(guī)則就能精準(zhǔn)判斷 this 指向。
2.1 場(chǎng)景 1:普通函數(shù)調(diào)用(無(wú)任何綁定)
調(diào)用形式:直接通過(guò)函數(shù)名()調(diào)用,不掛載在任何對(duì)象上。
綁定規(guī)則:
非嚴(yán)格模式:this 綁定到全局對(duì)象(瀏覽器是
window,Node 是global);嚴(yán)格模式(
use strict):this 綁定到undefined(禁止自動(dòng)綁定全局對(duì)象)。
代碼驗(yàn)證:
// 非嚴(yán)格模式
function normalCall() {
console.log("非嚴(yán)格模式:", this); // window(瀏覽器)
}
normalCall();
// 嚴(yán)格模式
function strictCall() {
"use strict";
console.log("嚴(yán)格模式:", this); // undefined
}
strictCall();
// 坑點(diǎn):函數(shù)內(nèi)嵌套函數(shù),仍按普通調(diào)用處理
function outer() {
console.log("outer this:", this); // window(非嚴(yán)格模式)
function inner() {
console.log("inner this:", this); // window(非嚴(yán)格模式)
}
inner(); // 普通調(diào)用,與outer的this無(wú)關(guān)
}
outer();
避坑點(diǎn):
? 不要在普通函數(shù)中依賴
this獲取全局對(duì)象(嚴(yán)格模式下會(huì)報(bào)錯(cuò));? 如需訪問(wèn)全局對(duì)象,瀏覽器用
window,Node 用globalThis(通用)。
2.2 場(chǎng)景 2:對(duì)象方法調(diào)用(掛載在對(duì)象上調(diào)用)
調(diào)用形式:通過(guò)對(duì)象.函數(shù)名()調(diào)用,函數(shù)作為對(duì)象的屬性存在。
綁定規(guī)則:this 綁定到 “調(diào)用該方法的對(duì)象”(即.前面的對(duì)象)。
代碼驗(yàn)證:
const user = {
name: "張三",
sayHi() {
console.log("this指向:", this); // 指向user對(duì)象
console.log("用戶名:", this.name); // 輸出“張三”
},
address: {
city: "北京",
getCity() {
console.log("城市:", this.city); // 指向address對(duì)象,輸出“北京”
}
}
};
// 直接調(diào)用對(duì)象方法 → this指向?qū)ο?
user.sayHi();
user.address.getCity();
// 坑點(diǎn)1:方法賦值給變量后,變成普通調(diào)用
const sayHi = user.sayHi;
sayHi(); // 普通調(diào)用 → this指向window,name為undefined
// 坑點(diǎn)2:嵌套對(duì)象中,this指向直接調(diào)用的對(duì)象(非外層)
user.address.getCity.call(user); // 強(qiáng)制改變this為user,輸出undefined(user無(wú)city屬性)
關(guān)鍵原理:
this 綁定的是 “直接調(diào)用者”,而非 “函數(shù)定義時(shí)所在的對(duì)象”。即使函數(shù)定義在其他地方,只要通過(guò)obj.fn()調(diào)用,this 就指向obj:
// 函數(shù)定義在外部
function getUserName() {
console.log(this.name);
}
// 掛載到不同對(duì)象調(diào)用
const obj1 = { name: "李四", getUserName };
const obj2 = { name: "王五", getUserName };
obj1.getUserName(); // 輸出“李四”(this指向obj1)
obj2.getUserName(); // 輸出“王五”(this指向obj2)
2.3 場(chǎng)景 3:構(gòu)造函數(shù)調(diào)用(new 關(guān)鍵字)
調(diào)用形式:通過(guò)new 函數(shù)名()創(chuàng)建實(shí)例。
綁定規(guī)則:this 綁定到 “新創(chuàng)建的實(shí)例對(duì)象”。
代碼驗(yàn)證:
function Person(name, age) {
// new調(diào)用時(shí),this指向新創(chuàng)建的Person實(shí)例
this.name = name;
this.age = age;
console.log("構(gòu)造函數(shù)this:", this); // Person { name: "...", age: ... }
}
// new調(diào)用 → this綁定到實(shí)例
const p1 = new Person("趙六", 25);
console.log(p1.name); // 輸出“趙六”
// 坑點(diǎn):構(gòu)造函數(shù)返回對(duì)象會(huì)覆蓋this
function PersonWithReturn(name) {
this.name = name;
// 返回對(duì)象時(shí),new創(chuàng)建的實(shí)例會(huì)被這個(gè)對(duì)象替代
return { name: "錢七" };
}
const p2 = new PersonWithReturn("孫八");
console.log(p2.name); // 輸出“錢七”(this被覆蓋)
// 注意:返回基本類型(如number、string)不會(huì)覆蓋this
function PersonWithPrimitive(name) {
this.name = name;
return 123; // 基本類型,不影響this
}
const p3 = new PersonWithPrimitive("周九");
console.log(p3.name); // 輸出“周九”
new 調(diào)用的底層流程:
創(chuàng)建一個(gè)新的空對(duì)象(
const obj = {});將新對(duì)象的
__proto__指向構(gòu)造函數(shù)的prototype(實(shí)現(xiàn)繼承);調(diào)用構(gòu)造函數(shù),將 this 綁定到新對(duì)象;
若構(gòu)造函數(shù)返回對(duì)象,則返回該對(duì)象;否則返回新對(duì)象。
2.4 場(chǎng)景 4:apply/call/bind 綁定(強(qiáng)制改變 this)
調(diào)用形式:通過(guò)fn.apply(obj)、fn.call(obj)、fn.bind(obj)調(diào)用。
綁定規(guī)則:this 強(qiáng)制綁定到傳入的obj(obj為null/undefined時(shí),非嚴(yán)格模式綁定全局,嚴(yán)格模式綁定obj)。
三者區(qū)別:
| 方法 | 調(diào)用形式 | 是否立即執(zhí)行 | 參數(shù)傳遞方式 |
|---|---|---|---|
| apply | fn.apply(obj, [arg1, arg2]) | 是 | 數(shù)組傳遞參數(shù) |
| call | fn.call(obj, arg1, arg2) | 是 | 逗號(hào)分隔傳遞參數(shù) |
| bind | const newFn = fn.bind(obj) | 否 | 返回新函數(shù),延遲執(zhí)行 |
代碼驗(yàn)證:
function sayInfo(age, city) {
console.log(`姓名:${this.name},年齡:${age},城市:${city}`);
}
const user = { name: "吳十" };
// apply調(diào)用 → 數(shù)組傳參,立即執(zhí)行
sayInfo.apply(user, [28, "上海"]); // 輸出“姓名:吳十,年齡:28,城市:上?!?
// call調(diào)用 → 逗號(hào)傳參,立即執(zhí)行
sayInfo.call(user, 28, "上海"); // 輸出同上
// bind調(diào)用 → 返回新函數(shù),延遲執(zhí)行
const boundSayInfo = sayInfo.bind(user, 28);
boundSayInfo("上海"); // 輸出同上
// 坑點(diǎn):bind是硬綁定,后續(xù)無(wú)法被apply/call修改
const newObj = { name: "鄭十一" };
boundSayInfo.call(newObj, "北京"); // 仍輸出“吳十”(this無(wú)法改變)
特殊情況:obj 為 null/undefined
// 非嚴(yán)格模式:obj為null/undefined時(shí),this綁定全局
sayInfo.call(null, 28, "廣州"); // 姓名:undefined(window.name為空)
// 嚴(yán)格模式:obj為null/undefined時(shí),this綁定obj本身
function strictSayInfo() {
"use strict";
console.log(this);
}
strictSayInfo.call(null); // 輸出null
strictSayInfo.call(undefined); // 輸出undefined
2.5 場(chǎng)景 5:箭頭函數(shù)(無(wú)獨(dú)立 this)
調(diào)用形式:const fn = () => { ... }(無(wú)function關(guān)鍵字)。
綁定規(guī)則:箭頭函數(shù)沒(méi)有獨(dú)立的 this,其 this 繼承自 “外層執(zhí)行上下文的 this”(定義時(shí)的外層,非調(diào)用時(shí))。
核心特性:
無(wú)法通過(guò)
apply/call/bind改變 this(綁定后仍為外層 this);不能作為構(gòu)造函數(shù)(用 new 調(diào)用會(huì)報(bào)錯(cuò));
沒(méi)有
arguments對(duì)象(需用剩余參數(shù)...args替代)。
代碼驗(yàn)證:
// 場(chǎng)景1:箭頭函數(shù)作為對(duì)象方法 → this繼承外層(window)
const obj = {
name: "王十二",
sayHi: () => {
console.log(this.name); // undefined(this指向window)
}
};
obj.sayHi();
// 場(chǎng)景2:箭頭函數(shù)嵌套在對(duì)象方法中 → 繼承方法的this
const obj2 = {
name: "李十三",
outer() {
const inner = () => {
console.log(this.name); // 繼承outer的this,指向obj2,輸出“李十三”
};
inner();
}
};
obj2.outer();
// 場(chǎng)景3:箭頭函數(shù)無(wú)法被apply/call改變this
const arrowFn = () => {
console.log(this);
};
arrowFn.call({ name: "張十四" }); // 輸出window(非嚴(yán)格模式),無(wú)法改變
// 場(chǎng)景4:箭頭函數(shù)不能作為構(gòu)造函數(shù)
const ArrowPerson = () => {};
new ArrowPerson(); // 報(bào)錯(cuò):ArrowPerson is not a constructor
適用場(chǎng)景:
嵌套函數(shù)中需要繼承外層 this(如定時(shí)器、回調(diào)函數(shù));
避免普通函數(shù)中 this 綁定全局的問(wèn)題(如 React 類組件的事件回調(diào))。
2.6 場(chǎng)景 6:DOM 事件回調(diào)與 class 中的 this
(1)DOM 事件回調(diào)
調(diào)用形式:dom.addEventListener('click', fn)。
綁定規(guī)則:this 綁定到 “觸發(fā)事件的 DOM 元素”(即事件源)。
const btn = document.createElement("button");
btn.textContent = "點(diǎn)擊我";
document.body.appendChild(btn);
// 普通函數(shù) → this指向btn
btn.addEventListener("click", function() {
console.log(this); // <button>點(diǎn)擊我</button>
});
// 坑點(diǎn):箭頭函數(shù) → this繼承外層(window)
btn.addEventListener("click", () => {
console.log(this); // window(無(wú)法獲取btn)
});
(2)class 中的 this
綁定規(guī)則:class 內(nèi)部默認(rèn)啟用嚴(yán)格模式,方法中的 this 默認(rèn)綁定到實(shí)例,但脫離實(shí)例調(diào)用時(shí)為undefined。
class User {
constructor(name) {
this.name = name;
}
sayHi() {
console.log(this.name);
}
}
const user = new User("劉十五");
user.sayHi(); // 輸出“劉十五”(this指向?qū)嵗?
// 坑點(diǎn):方法賦值后調(diào)用 → 嚴(yán)格模式下this為undefined
const sayHi = user.sayHi;
sayHi(); // 報(bào)錯(cuò):Cannot read properties of undefined (reading 'name')
// 解決方案:在constructor中綁定this
class UserWithBind {
constructor(name) {
this.name = name;
// 綁定this到實(shí)例
this.sayHi = this.sayHi.bind(this);
}
sayHi() {
console.log(this.name);
}
}
const user2 = new UserWithBind("陳十六");
const sayHi2 = user2.sayHi;
sayHi2(); // 輸出“陳十六”(this已綁定)
三、避坑指南:8 個(gè)高頻 this 指向錯(cuò)誤及解決方案
掌握規(guī)則后,還要能識(shí)別實(shí)際開(kāi)發(fā)中的 “隱形陷阱”,以下是 8 個(gè)最容易踩的坑及解決方法。
3.1 坑 1:對(duì)象方法賦值給變量后調(diào)用,this 指向錯(cuò)誤
錯(cuò)誤代碼:
const obj = {
name: "趙十七",
sayHi() {
console.log(this.name);
}
};
const hi = obj.sayHi;
hi(); // undefined(this指向window)
解決方案:
直接通過(guò)對(duì)象調(diào)用:
obj.sayHi();用 bind 綁定 this:
const hi = obj.sayHi.bind(obj); hi();用箭頭函數(shù)包裹:
const hi = () => obj.sayHi(); hi()。
3.2 坑 2:定時(shí)器回調(diào)中 this 指向全局
錯(cuò)誤代碼:
const obj = {
name: "孫十八",
delaySayHi() {
setTimeout(function() {
console.log(this.name); // undefined(this指向window)
}, 1000);
}
};
obj.delaySayHi();
解決方案:
用箭頭函數(shù)(繼承外層 this):
setTimeout(() => { console.log(this.name); // 輸出“孫十八” }, 1000);保存 this 到變量:
const self = this; setTimeout(function() { console.log(self.name); // 輸出“孫十八” }, 1000);
3.3 坑 3:數(shù)組 forEach/map 中的 this 指向錯(cuò)誤
錯(cuò)誤代碼:
const obj = {
prefix: "編號(hào):",
processArr(arr) {
return arr.map(function(item) {
return this.prefix + item; // undefined(this指向window)
});
}
};
obj.processArr([1, 2, 3]); // ["undefined1", "undefined2", "undefined3"]
解決方案:
用箭頭函數(shù):
arr.map(item => this.prefix + item);
傳 this 作為 forEach/map 的第二個(gè)參數(shù):
arr.map(function(item) { return this.prefix + item; }, this); // 第二個(gè)參數(shù)綁定this
3.4 坑 4:class 方法作為事件回調(diào),this 為 undefined
錯(cuò)誤代碼:
class Button {
constructor() {
this.text = "點(diǎn)擊";
this.btn = document.createElement("button");
this.btn.textContent = this.text;
this.btn.addEventListener("click", this.handleClick);
}
handleClick() {
console.log(this.text); // undefined(this為undefined,嚴(yán)格模式)
}
}
new Button();
解決方案:
constructor 中 bind 綁定:
constructor() { // ... this.handleClick = this.handleClick.bind(this); }用箭頭函數(shù)作為回調(diào):
this.btn.addEventListener("click", (e) => this.handleClick(e));
3.5 坑 5:箭頭函數(shù)作為對(duì)象方法,this 指向錯(cuò)誤
錯(cuò)誤代碼:
const obj = {
name: "周十九",
sayHi: () => {
console.log(this.name); // undefined(this指向window)
}
};
obj.sayHi();
解決方案:
放棄箭頭函數(shù),用普通函數(shù)作為對(duì)象方法:
sayHi() { console.log(this.name); // 輸出“周十九” }
3.6 坑 6:構(gòu)造函數(shù)返回對(duì)象,this 被覆蓋
錯(cuò)誤代碼:
function Product(name) {
this.name = name;
// 錯(cuò)誤:返回對(duì)象覆蓋this
return { name: "默認(rèn)商品" };
}
const phone = new Product("手機(jī)");
console.log(phone.name); // 輸出“默認(rèn)商品”
解決方案:
不返回對(duì)象,或返回 this:
function Product(name) { this.name = name; return this; // 或不寫return }若需返回額外數(shù)據(jù),掛載到 this 上:
function Product(name) { this.name = name; this.extra = { price: 999 }; // 額外數(shù)據(jù)掛載到this }
3.7 坑 7:bind 多次綁定,只有第一次生效
錯(cuò)誤代碼:
function fn() {
console.log(this.name);
}
const obj1 = { name: "吳二十" };
const obj2 = { name: "鄭二十一" };
// 多次bind,只有第一次生效
const bound1 = fn.bind(obj1);
const bound2 = bound1.bind(obj2);
bound2(); // 輸出“吳二十”(obj2綁定無(wú)效)
解決方案:
- 避免多次 bind,如需動(dòng)態(tài)改變 this,用 apply/call(而非 bind)。
3.8 坑 8:嚴(yán)格模式與非嚴(yán)格模式混用,this 指向混亂
錯(cuò)誤代碼:
// 外層非嚴(yán)格模式
function outer() {
"use strict"; // 內(nèi)層嚴(yán)格模式
function inner() {
console.log(this); // undefined(嚴(yán)格模式)
}
inner();
}
outer();
解決方案:
項(xiàng)目中統(tǒng)一嚴(yán)格模式(推薦),在入口文件或模塊頂部添加
"use strict";避免函數(shù)內(nèi)部局部啟用嚴(yán)格模式,導(dǎo)致 this 行為不一致。
四、總結(jié):this 指向的判斷流程(萬(wàn)能公式)
遇到任何 this 指向問(wèn)題,都可以按以下步驟判斷,準(zhǔn)確率 100%:
函數(shù)是否為箭頭函數(shù)?
→ 是:this 繼承外層執(zhí)行上下文的 this(直接找外層 this);
→ 否:進(jìn)入下一步。函數(shù)是否用 new 調(diào)用?
→ 是:this 指向新創(chuàng)建的實(shí)例;
→ 否:進(jìn)入下一步。函數(shù)是否用 apply/call/bind 綁定?
→ 是:this 指向綁定的對(duì)象(obj 為 null/undefined 時(shí),非嚴(yán)格模式綁全局,嚴(yán)格模式綁 obj);
→ 否:進(jìn)入下一步。函數(shù)是否作為對(duì)象方法調(diào)用(obj.fn ())?
→ 是:this 指向調(diào)用方法的對(duì)象(obj);
→ 否:進(jìn)入下一步。是否為嚴(yán)格模式(普通調(diào)用)?
→ 是:this 綁定到 undefined;
→ 否:this 綁定到全局對(duì)象(window/global)。
五、最后:this 的設(shè)計(jì)意義
為什么 JavaScript 要設(shè)計(jì) this?本質(zhì)是為了 “代碼復(fù)用”—— 讓函數(shù)可以在不同對(duì)象上調(diào)用,無(wú)需為每個(gè)對(duì)象重復(fù)定義相同邏輯。
比如一個(gè)sayHi函數(shù),通過(guò) this 可以在不同用戶對(duì)象上復(fù)用,輸出不同用戶名:
function sayHi() {
console.log(`Hi, ${this.name}`);
}
const userA = { name: "用戶A", sayHi };
const userB = { name: "用戶B", sayHi };
userA.sayHi(); // Hi, 用戶A
userB.sayHi(); // Hi, 用戶B
理解 this 的設(shè)計(jì)初衷,才能更好地運(yùn)用它。希望本文能幫你告別 “this 困惑”,寫出邏輯清晰、無(wú)隱藏 bug 的 JavaScript 代碼。
到此這篇關(guān)于JavaScript中this取值的文章就介紹到這了,更多相關(guān)JS this取值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過(guò)復(fù)制Table生成word和excel的javascript代碼
通過(guò)復(fù)制Table生成word和excel,個(gè)人感覺(jué)這個(gè)功能還是比較實(shí)用的,下面有個(gè)不錯(cuò)的示例,希望對(duì)大家有所幫助2014-01-01
跨瀏覽器通用、可重用的選項(xiàng)卡tab切換js代碼
今天一同學(xué)對(duì)我說(shuō)“好吧,我準(zhǔn)備去學(xué)習(xí)”,我大驚,這老勾引我打dota的也去學(xué)習(xí),于是我好奇他學(xué)什么,他說(shuō)要搞一個(gè)選項(xiàng)卡切換js2011-09-09
使用JavaScript清除cookie的方法總結(jié)
在現(xiàn)代Web開(kāi)發(fā)中,清除Cookie是維護(hù)網(wǎng)站用戶隱私和安全性的一個(gè)重要步驟,JavaScript提供了幾種方法來(lái)清除Cookie,包括直接刪除特定的Cookie、設(shè)置Cookie的過(guò)期時(shí)間為過(guò)去的時(shí)間點(diǎn)、以及使用第三方庫(kù)來(lái)輔助清除,本文將詳細(xì)的給大家介紹這些方法,需要的朋友可以參考下2025-04-04
小程序?qū)崿F(xiàn)訂單評(píng)價(jià)和商家評(píng)價(jià)
這篇文章主要為大家詳細(xì)介紹了小程序?qū)崿F(xiàn)訂單評(píng)價(jià)和商家評(píng)價(jià)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
JavaScript中實(shí)現(xiàn)PHP的打亂數(shù)組函數(shù)shuffle實(shí)例
這篇文章主要介紹了JavaScript中實(shí)現(xiàn)PHP的打亂數(shù)組函數(shù)shuffle實(shí)例,本文用2種方法實(shí)現(xiàn)了類似PHP的打亂數(shù)組函數(shù)shuffle函數(shù),需要的朋友可以參考下2014-10-10
jfinal與bootstrap的登錄跳轉(zhuǎn)實(shí)戰(zhàn)演習(xí)
這篇文章給大家分享jfinal與bootstrap之間的登錄跳轉(zhuǎn),具體內(nèi)容包含有點(diǎn)擊登錄彈出模態(tài)框、點(diǎn)擊登錄確認(rèn)按鈕后的validate、jfinal的validate、jfinal的session管理、ajax請(qǐng)求與返回信息處理、頁(yè)面間智能跳轉(zhuǎn)。感興趣的朋友跟著小編一起看看吧2015-09-09

