javascript 返回值
<script>
var v=123;
function foo(){
var v=456;
function inner(){
console.log(v)
}
return inner
}
result = foo()
console.log(result)
</script>
原因:
return inner 中inner是一个对象,当执行return inner时,会自动返回inner对象。
javascript 作用域
<script>
var v=123;
function foo(){
var v=456;
function inner(){
console.log(v)
}
return inner
}
result = foo()
console.log(result())
</script>
原因:
result() 当返回inner对象时,会自动执行inner对象,先输出456,由于inner没有返回值,执行到return inner()
返回空值,空值在javascript中是undefined
在编译的过程中,一个函数就是一个作用域,且作用域已经生成
javascript 面向对象/this/自执行函数
<script>
Name='root';
Age= 18;
function Foo(name,age){
this.Name = name;
this.Age = age;
this.Func = function(){
console.log(this.Name,this.Age);
(function(){
console.log(this.Name,this.Age);
})();
}
}
obj = new Foo('alex',666)
obj.Func()
</script>
js面向对象
js没有类,直接拿函数充当类,
# 创建函数
function func(){
}
# 创建类
function Func(){
this.Name = name;
this.Age = age;
}
# 实例化类
obj = new Func('root',18)
this关键字
1. 每个函数中都有this
2. 函数调用时,this = window
3. 类new时, this = obj
function Func(){
# 当做函数执行函数时,this=window 全局变量的对象
console.log(this)
}
obj = new Func()
window
var v=6
alert(window.v)
js中无字典,只有对象
v = {
Name:'root',
Age:18,
Func:function(){
}
}
相当月new了对象 obj
obj = {
Name:'root',
Func:function(){
console.log(this.Name)
}
}
obj.Func()
Name='alex'
obj = {
Name:'root',
Func:function(){
# this=obj
console.log(this.Name) # root
function inner(){
# this= window
console.log(this.Name) #alex
}
inner() # 相当于window.inner()
}
}
Name='alex'
obj = {
Name:'root',
Func:function(){
# this=obj
console.log(this.Name) # root
var that = this;
function inner(){
# this= window
console.log(this.Name) #root
}
inner() # 相当于window.inner()
}
}
自执行函数
(function){
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。