外部类无法访问内部类
外部类想要访问内部类需要闭包
两个函数可以有相同的变量名
两个函数包含关系,访问被包含的函数,在其该函数的外边可访问
如果外部函数存在这个同名的函数变量,则内部函数会屏蔽外部函数的
规范:先声明之后在应用
- function qu (){
- let x = x = 1,
- y=x + 1,
- z,i;
- }
规范应用
- 'use strict'
- //用于将javascript代码规范化
默认所有的全局变量都绑定在window对象上
- let name = 'xxx';
- alert(name);
- alert(window.name);
- 第二个和第一个都是一样的
alert()这个函数本身也是一个window的变量
- let v = 'xxx';
- window.alert(v);
- let old-alert = window.alert;
- window.alert = function(){
- };//这里将alert作用域定义到方法中。
- //解救的方法
- window.alert = old-alert;//将作用域变到外部
javascript实际上只有一个全局的作用域,任何的变量(函数也可以视为变量),如果没有函数的作用范围内找到,就会在全局作用域没有找到,报错
为更好的定义常变量,统一将常变量封装到方法中,
- //唯一的全局变量
- let Name = {};
- //定义全局变量
- Name.name = 'lala';
- Name.add = function(a,v){
- return a + v;
- }
var无法解决局部作用域的冲突问题,故引用let
ES6常量引用关键字const
内部函数和外部函数的变量重名时,内部函数的变量会覆盖外部函数的变量
new Date().getFullYear()获取此刻的时间
- let Kun = {
- name: '手东',
- birth : 2020,
- age: function(){
- let none = new Date().getFullYear();
- return none - this.birth;
- }
- }
this是无法指向的,是默认指向调用它的那个对象,而apply可以控制this的指向
getage.apply(getAge , [])括号第一个指定调用那个函数,第二个是参数,用于声明空参数
- let getage = {
- name: '世界',
- birth: 2000,
- age: getage
- };
- getage.apply(getAge , [])
- }
获取时分秒
- var now = new Date();
- now.getFullYear();//年
- now.getMonth();//月
- now.getDate();//日
- now.getDay();//星期几
- now.getHours();//时
- now.getMinutes();//分
- now.getTime();// 秒
- 全球统一的时间
- now.getTime();
- console.log(new Date(1578106175991))