Comments (10)
关于函数声明和变量声明覆盖问题
foo() // 2
var foo = function() {console.log(1)}
function foo() {console.log(2)}
foo() // 2
function foo() {console.log(2)}
var foo = function() {console.log(1)}
var foo = function() {console.log(1)}
function foo() {console.log(2)}
foo() // 1
问题:发生了什么???
from 2018.10.
function优先于 var
from 2018.10.
我的理解....第一段和第二段代码实际执行顺序是
function foo () {
console.log(2);
}
var foo;
foo();
foo = function () {
console.log(1);
}
变量和函数都会提升,但是函数优先。
第三段代码实际顺序是
function foo () {
console.log(2)
}
var foo;
foo = function () {
console.log(1)
}
foo ();
foo函数相当于被重新定义了(不知道这个描述是否准确),在这些例子中,不管函数提升还是变量提升,都不会影响其他代码的执行顺序。至于会不会有可能会影响的情况....我也说不好
from 2018.10.
`Base.extend = function (props, methods) {
function Child() {
Base.call(this)
}
Child.prototype = new this;
for (let i in props) {
Child.prototype[i] = props[i];
}
for (let i in methods) {
Child[i] = methods[i];
}
Child.extend = this.extend;
return Child;
}`
- Child.prototype = new this; 这里不是太理解,没有用过new this这种语法,想知道实际做了什么
- 属性赋值是用的prototype, method却是直接赋值的没有用prototype,这里的区别是不是就只是想把属性全部暴露给字类,而方法就只是想暴露给这个child子类。
求指导。
from 2018.10.
@fefuns 如果函数优先的话,var foo 不是会覆盖了function foo吗?foo()的话会抛出TypeError
from 2018.10.
@boomyao 函数提升优先级比变量提升要高,且不会被变量声明覆盖,但是会被变量赋值覆盖。
from 2018.10.
关于函数声明和变量声明覆盖问题
foo() // 2 var foo = function() {console.log(1)} function foo() {console.log(2)}
foo() // 2 function foo() {console.log(2)} var foo = function() {console.log(1)}
var foo = function() {console.log(1)} function foo() {console.log(2)} foo() // 1
问题:发生了什么???
上面三段代码实际上可以这样理解:
第一段
function foo() {
console.log( 2 );
}
// var foo 重复定义被忽略
foo(); // 2
foo = function() {
console.log( 1 );
}
第二段:
function foo() {console.log(2)}
// var foo 重复定义被忽略
foo() // 2
foo = function() {console.log(1)}
第三段:
function foo() {console.log(2)}
// var foo 重复定义被忽略
foo = function() {console.log(1)}
foo() // 1
from 2018.10.
@boomyao 函数提升优先级比变量提升要高,且不会被变量声明覆盖,但是会被变量赋值覆盖。
是函数声明提升的优先级高于函数表达式和变量赋值表达式。
函数声明:
function foo() {
console.log(1)
}
函数表达式:
var a = function() {
console.log(2)
}
变量赋值表达式:
var a = 1;
from 2018.10.
var name = 'A';
function getName () {
return this.name
}
var obj = {
name: 'B',
showName: function (a) {
console.log(getName());
console.log(a());
console.log(arguments[0]());
}
}
obj.showName(getName, 1);
from 2018.10.
在js中面向对象是不是必须的:
- 有哪些非面向对象的实践;
- 面向对象相对这些非面向对象的实践有啥优缺点
from 2018.10.
Related Issues (20)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 2018.10.