每天一道 JS 面试题
1、定义 var a = {n: 1}; var b = a;
,a.x=a={n: 2}
, 此时 console.log(a.x); console.log(b);
, 分别打印出什么?
2、检查给定的一个参数是 String
类型,只适用于 string promitives.使用 typeof 检查被分类为 string primitive 的值。
每天一道 JS 面试题
每天一道 JS 面试题
1、定义 var a = {n: 1}; var b = a;
,a.x=a={n: 2}
, 此时 console.log(a.x); console.log(b);
, 分别打印出什么?
2、检查给定的一个参数是 String
类型,只适用于 string promitives.使用 typeof 检查被分类为 string primitive 的值。
var a = {n: 1}; var b = a;
,a.x=a={n: 2}
, 此时 console.log(a.x); console.log(b);
, 分别打印出什么?题目出处:未知
知识点:js连续赋值、栈和堆内存的管理
答案:
var a = { n: 1 };
var b = a;
// 一般赋值运算为从右到左,但 a.x 的运算优先级比较高,优先运算, 第一步获取到 a.x 不存在,js在 `堆中` 开辟新内存储存 a.x = undefined,
// 接着运行 a = {n: 2}; a 指向了另一个地址,并且这个表达式的范围值是 {n: 2}
// 接着 b.x -> {n: 1}.x = {n: 2} 此时b指向之前a的引用地址,但是 a 已经指向了 {n: 2} 新内存地址
// b = {n: 1, x: {n: 2}}
a.x = a = { n: 2 };
console.log(a.x); // 打印 undefined
console.log(b); // 打印 {n: 1, x: {n: 2}}
String
类型,只适用于 string promitives.使用 typeof 检查被分类为 string primitive 的值。const isString = val => typeof val === 'string';
isString('10'); // true
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.