首页 > Web开发 > 详细

js面试题

时间:2019-08-27 17:10:57      阅读:99      评论:0      收藏:0      [点我收藏+]

var v = 123;
function foo(){
var v = 456;
function inner(){
console.log(v)
}
return inner
}

result = foo()
console.log(result())

# 问输出结果:NULL

Name=‘root‘;
Age = 18;
function Foo(name,age){
this.Name = name;
this.Age = age;
this.Func = function(){
// this=obj
console.log(this.Name,this.Age);
(function(){
console.log(this.Name,this.Age);
})();
}
}
obj = new Foo(‘alex‘,666);
obj.Func()
# 问输出结果:
alex 666
root 18






1. js面向对象

function Func(name,age){
this.Name = name;
this.Age = age;
}
obj = new Func(‘root‘,18)

2. this关键字
# 每个函数中都有this
# 函数调用时,this=window
# 类new时, this=obj

function func(){
# 当做函数执行函数时,this=window
console.log(this);
}
func()


function Func(){
# his=obj
console.log(this);
}
obj = new Func()


3. js中无字典,只有对象

Name = ‘alex‘
obj = {
Name: ‘root‘,
Age: 18,
Func: function(){
# this=obj
console.log(this.Name) # root

function inner(){
# this=window
console.log(this.Name)#alex
}
inner()#函数的调用方式window
}
}
相当于new了对象 obj

obj.Func()就等于obj



Name = ‘alex‘
obj = {
Name: ‘root‘,
Age: 18,
Func: function(){
# this=obj
console.log(this.Name) # root
var that =this;#修改的地方,那么that这时也是obj
function inner(){
# 这里面this=window
console.log(that.Name)这里的that等于obj
}
inner()
// 子执行函数
(function(){
console.log(that.Name)
})()
}
}
相当于new了对象 obj

obj.Func()













js面试题

原文:https://www.cnblogs.com/wenghao/p/11419107.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!