标签:对象 -- read 协议 写入 浏览器 only === symbols
- 浏览器中的javascript指的是ES + Web提供的Api(dom bom)
- node环境中javascript指的是ES + node提供的Api
1. 在全局作用域中的函数,变量都是公用的.所有相同的变量名称重复命名都会冲突导致报错
2. 函数内部直接赋值变量会把变量提升为全局变量
var a = 3
function a() {
a = 2;
console.log(‘func a‘)
}
console.log(a())//TypeError: a is not a function
var a = 3
function test() {
a = 2;
console.log(a)
}
console.log(test())//2
function test() {
console.log(a) //未声明 报错
a = 2;
console.log(a)
}
console.log(test()) //a is not defined
console.log(a)
function test() {
a = 2;
console.log(a) //2
}
console.log(test())
console.log(a) //2
1. 函数内部使用var声明变量(局部变量)只能在函数作用域内使用,外部无法访问到
function test() {
var a = 2;
console.log(a) //2
}
console.log(test())
console.log(a)//a is not defined
1. {} 花括号包括的就是块级作用域
2. let 关键字可以绑定到所在的作用域中
3. var 关键字声明的变量会提升到最近的函数作用域当中(函数作用域)
var a=1;
function test() {
function test1() {
if (true) {
var a = 44;
console.log(a) //44
}
console.log(a) //44
}
test1()
console.log(a) //1
}
console.log(a) //1
console.log(test())
console.log(a) //1
const a = { a: 1 };
a.a= 2
console.log(a) //{ a: 2 }
const b=2;
b=3 //TypeError: Assignment to constant variable.
const arr=[1,2,3,4]
const [first,secend,...rest]=arr;
console.log(first)//1
console.log(rest)//[3,4]
const obj ={
name:‘aa‘,
age:18
}
const {name,age}=obj
console.log(name)//aa
console.log(age)//18
const obj ={
name:‘aa‘,
age:18,
[‘姓名‘]:‘哈哈‘
}
console.log(`字符串输出${obj.姓名}`)//字符串输出哈哈
const obj ={
name:‘aa‘,
age:18
}
let age1=obj=>{
return this
}
console.log(age1(obj))//{}
const obj ={
name:‘aa‘,
age:18,
[‘姓名‘]:‘哈哈‘
}
let age1=obj=>{
return obj.姓名
}
console.log(age1(obj)) //哈哈
const obj={
name:‘test‘
}
console.log(Object.assign({},obj)) //{ name: ‘test‘ }
console.log(Object.assign({},obj,obj1)) //{ name: ‘test‘, age: ‘18‘ }
console.log(Object.is(-0,+0)) //false
console.log(Object.is(NaN,NaN)) //true
console.log(-0===+0)//true
console.log(NaN===NaN)//false
const obj = {
name: ‘test‘
}
const newProxy = new Proxy(obj, {
get(targer, property) {
return property in targer ? targer[property] : ‘default‘
},
set(targer, property, value) {
if (property === "age")
if (!Number.isInteger(value))
throw new TypeError("错误的数据类型")
targer[property] = value
console.log(targer)
}
})
console.log(newProxy.name) //test
console.log(newProxy.xx) //default
newProxy.age = "哈哈" //TypeError: 错误的数据类型
console.log(newProxy.age)
const obj = {
name: ‘test‘
}
const newProxy = new Proxy(obj, {
get(targer, property) {
return property in targer ? targer[property] : ‘default‘
},
set(targer, property, value) {
if (property === "age")
if (!Number.isInteger(value))
throw new TypeError("错误的数据类型")
targer[property] = value
// console.log(targer)
},
delete(targer,property){
delete targer[property]
}
})
// console.log(newProxy.name)
// console.log(newProxy.xx)
newProxy.age = 18
delete newProxy.age
console.log(newProxy.age) // default
const list = [];
let arrProxy = new Proxy(list, {
set(targer, property, value) {
targer[property] = value
return true //返回写入成功,不写返回值会报错(‘set‘ on proxy: trap returned falsish for property ‘0‘)
}
})
arrProxy.push(200)
arrProxy.push({ name: ‘test‘ })
console.log(list)
const obj = {
name: ‘test‘
}
// console.log(‘name‘ in obj) //true
// console.log(delete obj[‘name‘]) //true
// console.log(Object.keys(obj))//[]
console.log(Reflect.has(obj,‘name‘))//true
console.log(Reflect.deleteProperty(obj,‘name‘))//true
console.log(Reflect.ownKeys(obj))//[]
function myCls() {
this.name = "测试"
}
myCls.prototype.say = function () {
return this.name
}
let my = new myCls()
console.log(my.say())//测试
class myCls2 extends myCls{
//静态方法
static create(no,name) {
return new myCls2(no,name)
}
constructor(no,name) {
super(name)
this._no = no
}
//实例方法
say() {
return this._no
}
}
// let my2 = new myCls2(‘测试2‘)
// console.log(my2.say())//测试2
let my2=myCls2.create(1,‘测试2‘)
console.log(my2.say())
let m=new Map()
m.set({name:123},90)
console.log(m)
let om=new Object()
om.s=99;
console.log(om)
function (nb :number){
return nb;
}
//数组类型
let arr:Array<number>=[1,2,3] //<> 指定成员类型
let arr:number[]=[1,3,4] //:number[] 效果一样
let arr:[string,number] //元组数据类型(固定长度数组)
//对象类型
let obj:{foo?:string,bar:number}
let obj:{[string]:string} //限制对象属性 key 必须是字符串 ,值也必须是字符串
//函数类型
let fn:(string,number)=>void =function(‘aa‘,1){} //指定2个参数类型分别是string 和number,指定无返回值(undefind)
//type 关键字设置类型
type str=string
//字面量类型
type s=string | number //string或者number类型
//maybe 设置可为空
const b: ?number = undefind
const b: number | null | void = undefind
TypeScript 是 JavaScript 的超集,扩展了 JavaScript 的语法
string number bool 类型在ts中可以为空(null)值的
优点:
缺点:
安装及使用
断言 数据类型选择 (编译时的类型选择)
let num = 123 as number
let numb = ‘123‘ as number //直接报错
/**类型 "string" 到类型 "number" 的转换可能是错误的,因为两种类型不能充分重叠。
如果这是有意的,请先将表达式转换为 "unknown"。
**/
//设置动态接口
interface CacheTs {
readonly name: string //只读
[prop: string]: any //属性名string 值可以为任何值
};
let cc: CacheTs = {
name: ‘张三‘,
processName: ‘测试‘
};
//cc.name=‘里斯‘ //报错无法分配到 "name" ,因为它是只读属性。
console.log(cc.name);//张三
enum book {
‘天文‘,
‘地理‘,
‘文学‘,
‘言情‘,
‘科幻‘
}
console.log(book[0]) //天文
只定义,不做具体的实现
interface people {
name: string | null | undefined
age: number | undefined
}
class man implements people {
name: string | null | undefined = undefined
age: number | undefined = undefined
sex: number | null | undefined = undefined
constructor(name: string, sex: number, age: number) {
this.name = name;
this.age = age;
this.sex = sex;
}
}
let p = new man("张三", 1, 18)
console.log(p) //man { name: ‘张三‘, age: 18, sex: 1 }
...未完待续
ECMAScript和JavaScript及TypeScript
标签:对象 -- read 协议 写入 浏览器 only === symbols
原文地址:https://www.cnblogs.com/zhuzy/p/13405895.html