七一、js常见问题

1.如何检查一个数字是否是整数?

  • 最简单的方法是判断除以1的余数是否为0.
function isInt(num) {
  return num % 1 === 0;
}

console.log(isInt(4)); // true
console.log(isInt(12.2)); // false
console.log(isInt(0.3)); // false

2.如何比较JavaScript中的两个对象?

  • 两个非基本类型的值,比如对象(包括函数和数组)都是通过引用的形式来访问。如果直接通过=来判断,那么只会简单的判断其引用地址是否相同,而不是它们实际对应的值。
    如果数组和字符串做比较,那么数组会通过逗号拼接转换为字符串。通过等号判断的时候,两个相同的数组不会相等,但是和相同数据的字符串比较反而相等。
var a = [1,2,3];
var b = [1,2,3];
var c = "1,2,3";

a == c;     // true
b == c;     // true
a == b;     // false

3.解释undefined和not defined的区别

在JavaScript中,如果你尝试使用不存在的还未申明的变量,JavaScript会抛出错误var name is not defined。但是如果你用typeof来查看其类型,会返回undefined。
声明和定义的区别:var x是一个声明,因为你并没有定义其具体的值,你只是声明其存在性。
var x; // declaring x
console.log(x); //output: undefined
var x = 1同时兼具声明和定义,我们也可以叫它初始化。在JavaScript中,每一个变量和函数声明都会被提升到顶部。
如果我们访问一个声明了但是未定义的变量,会返回undefined。
var x; // Declaration
if(typeof x === ‘undefined’) // Will return true
访问一个未声明未定义的变量,会返回not defined错误。
console.log(y); // Output: ReferenceError: y is not defined

4. JavaScript中闭包是什么?

闭包是一个定义在其它函数(父函数)里面的函数,它拥有对父函数里面变量的访问权。闭包拥有如下三个作用域的访问权:

  1. 自身的作用域
  2. 父作用域
  3. 全局作用域
var globalVar = "abc";

// Parent self invoking function
(function outerFunction (outerArg) { // begin of scope outerFunction
  // Variable declared in outerFunction function scope
  var outerFuncVar = 'x';    
  // Closure self-invoking function
  (function innerFunction (innerArg) { // begin of scope innerFunction
    // variable declared in innerFunction function scope
    var innerFuncVar = "y";
    console.log(         
      "outerArg = " + outerArg + "\n" +
      "outerFuncVar = " + outerFuncVar + "\n" +
      "innerArg = " + innerArg + "\n" +
      "innerFuncVar = " + innerFuncVar + "\n" +
      "globalVar = " + globalVar);
  // end of scope innerFunction
  })(5); // Pass 5 as parameter
// end of scope outerFunction
})(7); // Pass 7 as parameter
输出结果如下:

outerArg = 7
outerFuncVar = x
innerArg = 5
innerFuncVar = y
globalVar = abc

  • innerFunction是一个闭包,定义在outerFunction中,它可以访问outerFunction作用域的所有变量。当然,它还可以访问全局变量。

5. 在JavaScript中如何创建私有变量?

function func() {
  var priv = "secret code";
  return function() {
    return priv;
  }
}

var getPriv = func();
console.log(getPriv()); // => secret code

6. this关键字如何工作?

  • 在JavaScript中,this总是指向函数的“拥有者”(也就是指向该函数的对象),或则拥有该函数的对象。
function foo() {
    console.log( this.bar );
}

var bar = "global";

var obj1 = {
    bar: "obj1",
    foo: foo
};

var obj2 = {
    bar: "obj2"
};

foo();          // "global"
obj1.foo();     // "obj1"
foo.call( obj2 );  // "obj2"
new foo();       // undefined

7.如何为Array对象添加你自定义的函数,计算平均值?

  • JavaScript是一个基于原型的语言。也就是说对象之间通过原型链接,并继承其函数。为了给Array对象添加函数,我们可以修改其原型定义Array prorotype。
Array.prototype.average = function() {
  // calculate sum
  var sum = this.reduce(function(prev, cur) { return prev + cur; });
  // return sum divided by number of elements
  return sum / this.length;
}

var arr = [1, 2, 3, 4, 5];
var avg = arr.average();
console.log(avg); //  3

8.JavaScript中提升(hoisting)是什么意思?

  • 提升(hoisting)是指JavaScript的解释器将所有的变量和函数声明都提升到该作用域的顶部,有两种提升类型:1.变量提升 2.函数提升
var a = 2;
foo();                 // works because `foo()`
                         // declaration is "hoisted"

function foo() {
    a = 3;
    console.log( a );   // 3
    var a;             // declaration is "hoisted"
                         // to the top of `foo()`
}

console.log( a );   // 2

9.实现一个promise

const isGreater = (a, b) => {
  return new Promise ((resolve, reject) => {
    if(a > b) {
      resolve(true)
    } else {
      reject(false)
    }
    })
}
isGreater(1, 2)
  .then(result => {
    console.log('greater')
  })
 .catch(result => {
    console.log('smaller')
 })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值