JavaScript-functions
This form of creating a function is also known as function expression. Unlike function declaration, function expressions are not hoisted.
匿名函数也称为函数表达式。函数表达式与函数声明有一些区别。函数声明会进行声明提升(declaration hoisting),而函数表达式不会。
hoist 是提升的意思,函数表达式不会被hoisted,
function declaration (函数声明) var
hoist 是提升的意思,函数表达式不会被hoisted,
function declaration (函数声明) var
function的scope,你创建一个function,变量和其它function内定义的things都在他们自己的scope里面,意味着他们都锁在各自的隔间(compartments)里面,所有不能从functions外面访问它们。
The top level outside all your functions is called the global scope.
Keeping parts of your code locked away in functions avoids such problems, and is considered the best practice.
The zoo keeper is like the global scope — they have the keys to access every enclosure, to restock food, tend to sick animals, etc.
zoo keeper 就像global scope,
textContent 和 innerHTML
let names = ['Chris', 'Li Kang', 'Anne', 'Francesca', 'Mustafa', 'Tina', 'Bert', 'Jada'] let para = document.createElement('p'); // Add your code here function chooseName(){ para.innerHTML += names[Math.floor(Math.random()*8)]; } chooseName(); // Don't edit the code below here! section.innerHTML = ' '; section.appendChild(para);
let names = ['Chris', 'Li Kang', 'Anne', 'Francesca', 'Mustafa', 'Tina', 'Bert', 'Jada'] let para = document.createElement('p'); // Add your code here function random(number){ return Math.floor(Math.random() * number) } function chooseName(arr) { let len = arr.length; return arr[random(len)]; } para.innerHTML = chooseName(names); // Don't edit the code below here! section.textContent = ' '; section.appendChild(para);
let canvas = document.querySelector('canvas'); let ctx = canvas.getContext('2d'); let x = 50; let y = 60; let width = 100; let height = 75; let color = 'blue'; // Add your code here function creatRectangle(){ // ctx.fillStyle = color; ctx.fillRect(x,y,width, height, color); } creatRectangle();
function myFunction() { alert('hello'); } #匿名函数 function() { alert('hello'); }
阅读量: 520
发布于:
修改于:
发布于:
修改于: