Front End

本章节主要说说我遇到的跟web前端有关的问题


1. JavaScript中的var

直到现在我也只能算是js的初学者,所以对于js也不敢说很懂,所以可能理解还是有错误。。。 这道题是我在面一家叫做YouVisit LLC的公司的面试题,面试岗位是Full Stack Developer,他貌似问我javaScriptvar的作用。。当时我基本没用过js所以直接大言不惭的说var没用,因为js是弱类型语言。。。现在往回看,var至少有2点作用:

  1. 这样写格式更加好,well formatted
  2. 在写function的时候,用来区分globallocal值,举个栗子

    function square(num) {
       total = num * num;
       return total;
    }
    
    var total = 50;
    var number = square(20);
    

    如果这样写的话,function里面的total全局变量。就会覆盖外面的total,所以应该在里面的total加上var


2. JavaScript中的Inheritance

其实现在也是刚开始学JavaScript,说的也不一定对。。大概讲讲,先定义一个class并定义一个成员函数:

function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
    console.log("Hi my name is " + this.name);
};

我们想定义一个PenguinClass继承Animal,首先定义PenguinClass

function Penguin(name) {
    this.name = name;
    this.numLegs = 2;
}

然后使用prototype,代码如下:

Penguin.prototype = new Animal();

完成继承。。


3. private in JS

举个栗子:

function Container(param) {
    this.member = param;
    var secret = 3;
}

这里面:

  • memberpublic
  • secretprivate

4. 生成随机数

严格来讲,这不算JavaScript的题,这应该算是所有语言通用的,不过whatever,还是放进来把。。

大致步骤:

  1. 通过时间生成seed
function rnd(seed) {
  var seed = (seed * 1234 + 567) % 1234567;
  return seed / 1234567.0
}

function rand(number) {
  var today = new Date();
  var seed = today.getTime();
  return ceil(rnd(seed) * number);
}

console.log(rand(number));

5. === & ==

===是严格相等,举例

4 === '4'  // false

== 是普通的相等

4 == '4'  // true

results matching ""

    No results matching ""