console.log(typeof searchTypeClick.value) console.log(searchTypeClick.value) console.log(searchTypeClick.value==true)第一行打印为boolean第二行打印为true,为什么第三行打印为false
时间: 2024-03-24 16:36:03 浏览: 17
根据你提供的代码和输出结果,我可以判断出 `searchTypeClick.value` 是一个布尔类型的值,而不是字符串类型。所以,第一行打印结果是错误的,应该打印 `typeof searchTypeClick.value` 的值是 "boolean"。
至于为什么第三行打印为false,这是因为 `searchTypeClick.value==true` 的比较方式不正确。布尔类型的值只有两个可能的取值,true 或 false。如果你想判断 `searchTypeClick.value` 是否为 true,应该写成 `searchTypeClick.value === true` 或者 `searchTypeClick.value`,因为布尔类型的值可以直接当做条件表达式使用,如果值为 true,条件表达式的结果就是 true,否则结果就是 false。因此,正确的代码应该是:
```
console.log(typeof searchTypeClick.value);
console.log(searchTypeClick.value);
console.log(searchTypeClick.value === true);
```
相关问题
promise.all,promise手写
Promise.all是一个方法,它接收一个promise对象组成的数组作为参数,当数组中所有的promise对象都变为fulfilled状态时,Promise.all返回一个新的promise对象,其状态为fulfilled,并将所有promise对象的结果组成一个数组作为该promise对象的结果。当数组中有一个promise对象变为rejected状态时,Promise.all返回一个新的promise对象,其状态为rejected,并将第一个被rejected的promise对象的结果作为该promise对象的结果。下面是一个使用Promise.all的例子:
```javascript
const promise1 = Promise.resolve(1);
const promise2 = Promise.resolve(2);
const promise3 = Promise.resolve(3);
Promise.all([promise1, promise2, promise3])
.then(values => {
console.log(values); // [1, 2, 3]
});
```
手写Promise的实现需要遵循Promise/A+规范,主要包括以下几个部分:
1. Promise构造函数:接收一个executor函数作为参数,executor函数接收两个函数参数resolve和reject,分别表示promise对象的状态变为fulfilled和rejected时的回调函数。
2. then方法:接收两个函数参数onFulfilled和onRejected,分别表示promise对象的状态变为fulfilled和rejected时的回调函数。then方法返回一个新的promise对象。
3. resolve方法:接收一个参数value,如果value是一个promise对象,则返回该promise对象;否则返回一个新的promise对象,其状态为fulfilled,并将value作为该promise对象的结果。
4. reject方法:接收一个参数reason,返回一个新的promise对象,其状态为rejected,并将reason作为该promise对象的结果。
下面是一个简单的手写Promise的实现:
```javascript
class MyPromise {
constructor(executor) {
this.status = 'pending';
this.value = undefined;
this.reason = undefined;
this.onFulfilledCallbacks = [];
this.onRejectedCallbacks = [];
const resolve = value => {
if (this.status === 'pending') {
this.status = 'fulfilled';
this.value = value;
this.onFulfilledCallbacks.forEach(callback => callback(this.value));
}
};
const reject = reason => {
if (this.status === 'pending') {
this.status = 'rejected';
this.reason = reason;
this.onRejectedCallbacks.forEach(callback => callback(this.reason));
}
};
try {
executor(resolve, reject);
} catch (error) {
reject(error);
}
}
then(onFulfilled, onRejected) {
onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value;
onRejected = typeof onRejected === 'function' ? onRejected : reason => { throw reason };
const promise2 = new MyPromise((resolve, reject) => {
if (this.status === 'fulfilled') {
setTimeout(() => {
try {
const x = onFulfilled(this.value);
this.resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
} else if (this.status === 'rejected') {
setTimeout(() => {
try {
const x = onRejected(this.reason);
this.resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
} else {
this.onFulfilledCallbacks.push(value => {
setTimeout(() => {
try {
const x = onFulfilled(value);
this.resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
});
this.onRejectedCallbacks.push(reason => {
setTimeout(() => {
try {
const x = onRejected(reason);
this.resolvePromise(promise2, x, resolve, reject);
} catch (error) {
reject(error);
}
}, 0);
});
}
});
return promise2;
}
resolvePromise(promise2, x, resolve, reject) {
if (promise2 === x) {
reject(new TypeError('Chaining cycle detected for promise'));
}
let called = false;
if (x !== null && (typeof x === 'object' || typeof x === 'function')) {
try {
const then = x.then;
if (typeof then === 'function') {
then.call(x, y => {
if (called) return;
called = true;
this.resolvePromise(promise2, y, resolve, reject);
}, r => {
if (called) return;
called = true;
reject(r);
});
} else {
resolve(x);
}
} catch (error) {
if (called) return;
called = true;
reject(error);
}
} else {
resolve(x);
}
}
static resolve(value) {
if (value instanceof MyPromise) {
return value;
}
return new MyPromise(resolve => resolve(value));
}
static reject(reason) {
return new MyPromise((resolve, reject) => reject(reason));
}
static all(promises) {
return new MyPromise((resolve, reject) => {
const results = [];
let count = 0;
promises.forEach((promise, index) => {
MyPromise.resolve(promise).then(value => {
results[index] = value;
count++;
if (count === promises.length) {
resolve(results);
}
}, reason => {
reject(reason);
});
});
});
}
}
```
模拟实现new操作符 const a1 = new A(arg1,arg2,...) const a2 = newFn(A,arg1,arg2,...) 并说明过程
要模拟实现new操作符,我们可以创建一个函数`newFn`来模拟`new`的行为。下面是一个示例实现:
```javascript
function newFn(constructor, ...args) {
// 创建一个空对象,并将其原型设置为构造函数的原型
const obj = Object.create(constructor.prototype);
// 在新对象上调用构造函数,并传入参数
const result = constructor.apply(obj, args);
// 如果构造函数返回一个对象,则返回该对象;否则返回新创建的对象
return (typeof result === 'object' && result !== null) ? result : obj;
}
```
这个`newFn`函数的实现步骤如下:
1. 创建一个空对象`obj`,并将其原型设置为构造函数`constructor`的原型。通过`Object.create(constructor.prototype)`来实现。
2. 在新对象上调用构造函数`constructor`,并传入参数`args`。使用`apply`方法来实现。
3. 检查构造函数的返回值。如果返回一个对象,则返回该对象;否则返回新创建的对象。
使用示例:
```javascript
function A(arg1, arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
const a1 = new A('value1', 'value2');
const a2 = newFn(A, 'value1', 'value2');
console.log(a1); // A { arg1: 'value1', arg2: 'value2' }
console.log(a2); // A { arg1: 'value1', arg2: 'value2' }
console.log(a1 instanceof A); // true
console.log(a2 instanceof A); // true
```
以上代码中,`a1`和`a2`都是通过构造函数`A`创建的实例,它们的值相同,并且都是`A`的实例。