`

JS Typical Questions

 
阅读更多

原创转载请注明出处:http://agilestyle.iteye.com/blog/2370610

 

Scope

(function() {
   var a = b = 5;
})();

console.log(a);
console.log(b);
ReferenceError: a is not defined
5

Note:

a 被关键字var声明,说明 a 是这个函数的局部变量,所以函数外打印 a, 会显示错误 not defined,

b 没有被关键字var声明,JS默认 b 是一个全局作用域,所以函数外打印 b,会显示 5

 

另外如果使用 'use strict'; 会直接报错

(function() {
   'use strict';
   var a = b = 5;
})();

console.log(a);
console.log(b);
ReferenceError: b is not defined

 

Create “native” methods

Define a repeatify function on the String object. The function accepts an integer that specifies how many times the string has to be repeated. The function returns the string repeated the number of times specified. For example: 

console.log('hello'.repeatify(3));

Should print hellohellohello

String.prototype.repeatify = String.prototype.repeatify || function(times) {
   var str = '';

   for (var i = 0; i < times; i++) {
      str += this;
   }

   return str;
};

console.log('hello'.repeatify(3));

 

Hoisting

function test() {
   console.log(a);
   console.log(foo());
   
   var a = 1;
   function foo() {
      return 2;
   }
}

test();
undefined
2

Note:

The reason is that both variables and functions are hoisted (moved at the top of the function) but variables don’t retain any assigned value. So, at the time the variable a is printed, it exists in the function (it’s declared) but it’s still undefined. 

  

How this works in JavaScript

var fullname = 'John Doe';
var obj = {
   fullname: 'Colin Ihrig',
   prop: {
      fullname: 'Aurelio De Rosa',
      getFullname: function() {
         return this.fullname;
      }
   }
};

console.log(obj.prop.getFullname());

var test = obj.prop.getFullname;

console.log(test());
Aurelio De Rosa
John Doe

Note:

The reason is that the context of a function, what is referred with the this keyword, in JavaScript depends on how a function is invoked, not how it’s defined.

In the first console.log() call, getFullname() is invoked as a function of the obj.prop object. So, the context refers to the latter and the function returns the fullname property of this object. On the contrary, when getFullname() is assigned to the test variable, the context refers to the global object (window). This happens because test is implicitly set as a property of the global object. For this reason, the function returns the value of a property called fullname of window, which in this case is the one the code set in the first line of the snippet. 

 

 

Reference

https://www.sitepoint.com/5-typical-javascript-interview-exercises/

分享到:
评论

相关推荐

    ftpd-typical.xml

    ftpd-typical.xml 用于ftpserver 配置说明

    Typical_100.cpp

    Typical_100.cpp

    Typical states and density matrices.pdf

    在量子力学中,量子态是描述物理系统状态的关键概念,而密度矩阵是这些状态的数学表示,尤其在处理混合态时至关重要。这篇由G.W. Gibbons和D.A.M. Thomas在1992年发表的文章《典型状态与密度矩阵》深入探讨了如何将...

    cpp-typical基于高度可组合类型的C模板元编程库

    cpp-typical是一个基于C++的模板元编程库,它的核心理念是提供高度可组合的类型工具,以增强C++的元编程能力。在C++中,模板元编程是一种在编译时进行计算的技术,它允许程序员创建能在编译期间生成代码的模板。...

    PyPI 官网下载 | typical-2.0.0b22-py3-none-any.whl

    "PyPI 官网下载 | typical-2.0.0b22-py3-none-any.whl" 这个标题表明我们关注的是一个在Python Package Index(PyPI)上发布的软件包。PyPI是Python社区的官方仓库,用于存储和分发Python软件包。这个特定的包名为...

    Python库 | typical-2.0.29-py3-none-any.whl

    标题中的"typical-2.0.29-py3-none-any.whl"就是一个Python库的轮子文件(wheel file),它是Python包的二进制分发格式,旨在简化安装过程。 首先,我们来理解一下"typical"这个库。尽管在Python生态系统中,...

    信号与系统教学课件:4-3 Frequency spectrum of typical signals.ppt

    《信号与系统》教学课件第4-3部分主要探讨了典型信号的频谱,具体涉及以下几个关键知识点: 1. **连续时间傅里叶变换(CTFT)**:CTFT是将时域信号转换到频域的一种分析工具,用于描述信号在频率上的分布。...

    Python库 | typical-2.0.0b8-py3-none-any.whl

    "typical-2.0.0b8-py3-none-any.whl" 就是这样一个Python库的轮子文件,它代表了一个特定版本的typical库,版本号为2.0.0b8。 首先,让我们深入了解一下Python的.whl文件。.whl是一种二进制分发格式,它是Python的...

    typical:接口定义语言

    典型的 典型的是接口定义语言。 安装 简易安装 如果要在x86-64 CPU上运行macOS或基于GNULinux,则可以使用以下命令安装“典型”: ...例如,以下将把Typical安装到工作目录中: curl https://raw.githubusercontent

    jspdaoguestbook_java_dao2006com_typical3xe_

    7. **版本控制**:"typical3xe"可能表示这是该系列的第三个典型版本,这可能意味着代码经过了多次迭代和改进,对于学习代码演化和版本管理也具有参考价值。 8. **项目结构**:通常,一个完整的Java Web项目会有一个...

    inscrfctions-typical.rar_mimo

    The typical application of MMSE algorithm for MIMO and OFDM channel estimation, contains detailed instructions

    06 Typical Processes in FusionCloud 6.3 Scenarios.pptx

    06 Typical Processes in FusionCloud 6.3 Scenarios.pptx

    yellow River Estuary typical wetlands classification based on hyperspectral

    ellow River Estuary typical wetlands classification based on hyperspectral derivative transformation

    ADS285 Typical Application

    ### ADS1285 32位Delta-Sigma ADC在地震学应用中的典型配置 #### 概述 ADS1285是一款专为地震学设备设计的32位低功耗模数转换器(ADC),它能够满足低噪声高精度数字转换以及延长电池使用寿命的需求。...

    UART.rar_UART VHDL_no key_typical7mc_uart

    下面将详细介绍UART的基本原理、VHDL实现的关键点以及与"no_key"和"typical7mc"相关的知识点。 UART基于异步通信协议,它不依赖时钟同步信号来传输数据,而是依靠起始位、数据位、奇偶校验位和停止位来识别数据帧的...

    adc_dma.zip_dsp adc_dsp adc实例_nailsssz_typical999

    在本文中,我们将深入探讨与标题“adc_dma.zip_dsp adc_dsp adc实例_nailsssz_typical999”相关的技术知识点,主要关注DSP(数字信号处理器)如何利用ADC(模数转换器)功能以及相关实例。描述指出,这是一个包含...

    EVALUATION OF ENVIRONMENTAL IMPACT OF TYPICAL LEATHER CHEMICALS. PART I:BIODEGRADABILITY OF FATLIQUORS UNDER ACTIVATED SLUDGE

    皮革行业作为众多发展中国家经济的重要组成部分,不仅在就业和出口方面起着关键作用,同时也因其排放大量的液体和固体废物而日益受到环境影响的关注。皮革加工过程中通常需要使用大量的化学品以确保皮革的质量,但...

    2017_2018学年高中英语每日一题第01周aim和typical的用法试题含解析新人教版选修6

    【aim】和【typical】这两个词在英语中是非常常见的,尤其在高中英语学习中扮演着重要角色。它们分别表示“目标”和“典型的”含义。 1. **aim**: - **名词形式**:aim 可以表示“目标”或“目的”。例如,...

    Node.js By Example

    Understand why architecting is important and what the planning of a typical Node.js application looks like Successfully manage every web application asset such as CSS, JavaScript, or image files ...

Global site tag (gtag.js) - Google Analytics