JS 型别 DAY52

动态型别

// JS属於 动态型别
// 执行阶段才会赋予确立型别

// typeof 检视型别
var name = '皮杰先生'
console.log(name); // 型别 string
console.log('皮杰先生'); // 型别 string

// 会发现 值的本身就具备型别 
// 而我们是把值赋予到变数上
// 变数的型别式来自於值的

型别转换

分为 显性转换 (Explicit conversion)隐性转换 (Implicit conversion)

// 显性转换
// 一个变数的值 直接被赋予另一个型别的值
var a = 1;
console.log(typeof a); // number
a = '1'
console.log(typeof a); // string
//隐性转换
var a = 1;
console.log(a , typeof a); // 1 number
a = a + '';
console.log(a , typeof a); // 1 string (number 转 string)
a = a * 5;
console.log(a , typeof a); // 5 number (string 转 number)


原始型别及物件型别

原始型别列表

  1. Boolean 布林
  2. Null 空值
  3. Undefined 未定义
  4. Number 数值
  5. String 字串
  6. BigInt(new) 整数数值(new)
  7. Symbol(new) Symbol(new)

上述这些原始型别都有各自的方法(ex:字串英文字转大写)
那为什麽他们有各自的方法呢???
因为他们有额外的包裹物件

  1. Boolean 布林 -- new Bollean()
  2. Null 空值
  3. Undefined 未定义
  4. Number 数值 - new Number()
  5. String 字串 - new String()
  6. BigInt(new) 整数数值(new) - BigInt()
  7. Symbol(new) Symbol(new) - Symbol()
    (Null , Undefined 没有包裹物件 )

这里来看 null 与 not defined 的型别

var d = null;
console.log(typeof d); // object

// JS长久以来的错误 但无法修正 因有的网站透过此错误来完成网站应用

console.log(name);
// name is not defined
console.log(typeof name);
// undefined
// 是 typeof 针对 not defined的保护措施

刚刚我们有稍微提到包裹物件
这边先举个例子

var a = 'Jay ';
console.log(a.length); // 4
console.log(a.toUpperCase()); // JAY
console.log(a.trim()); // 去头尾空白

那 a 究竟还有那些方法??
这时我们就可以利用包裹物件来查询

// 建构式宣告字串
var a = 'Jay';
var e = new String(a);
console.log(e); //请看下图
console.log(typeof e); // object

https://ithelp.ithome.com.tw/upload/images/20201014/201230395lD74jOLU1.jpg

这里的 proto 就是包裹物件的原型(方法皆在这)
但要注意
当我们宣告原始型别,要避免用建构式宣告
因为型别会是物件(object)

那今天的介绍就到这里
若有任何问题 或 内容有误
请跟我说唷/images/emoticon/emoticon07.gif


<<:  [Python][Arabic]阳春翻译工具

>>:  分享 : SpyGlass CDC 流程深入理解

html 改变按钮位子

昨天为止我们成功做出了多个按钮并且在鼠标移上时改变了他的效果,今天我们想要改变按钮的位子,让他可以靠...

我们的基因体时代-AI, Data和生物资讯 Day20-注释基因资讯的BED档案格式和bedtools

上一篇我们的基因体时代-AI, Data和生物资讯 Day19-分析和处理基因变异的档案格式VCF的...

[资料库] 学习笔记 - CTE 、时间函数 、 群组

这次解的题目是计算当月的每周平均工时,要以CTE的方式查询 详细的题目是从这篇文章延伸出来的,其他延...

RISC-V: I-type 位元运算指令

看到标题一定都猜到了, 相信大家对这几道指令都不陌生,这次实作的就是 ANDI、ORI、XORI, ...

[Day 29] 完成注册功能

在昨天取得了注册资讯 今天来把他写入DB里面 把前面几天的後台系统的MongoDB部分拿出来做使用 ...