1. div {
2. width: 300px;
3. height: 300px;
4. background-color: red;
5. }
1. <div></div>
2. <script>
3. var c = 10
4.
5. function test() {
6. var add1 = new Function('a,b', 'return a + b + c')
7. console.log(add1(3, 5))
8. // 使用Function构造函数可以通过字符串创建函数
9. // 注意:除非是用代码生成代码 否则不要这样写
10. // 通常写法 也是推荐的写法
11. function add2(a, b) {
12. return a + b + c
13. }
14. console.log(add2.length)
15. // 获取函数定义中的参数个数
16. console.log(add2(3, 5))
17. console.log(add2.toString())
18. // 可以获取函数的源代码 可以在运行时输出源代码
19.
20. // var code = prompt('请输入代码:', '')
21. // prompt 可提示用户进行输入的对话框
22. // console.log(code)
23.
24. var c = 10
25. // eval 函数可计算某个字符串 并执行其中的js代码
26. document.write(eval('5 + c'))
27.
28. }
29. test()
30.
31. document.querySelector('div').onclick = function (ev) {
32. console.log(this)
33. // 在事件处理函数中 this指向的是当前监听事件的标签元素
34. // 相当于ev.curentTarget
35. }
36.
37. var p = {
38. name: 'baoqiang',
39. age: 3,
40. sayHello: function (ev) {
41. console.log(this)
42. alert('我的名字是' + this.name)
43. }
44. }
45.
46. document.querySelector('div').onclick = p.sayHello.bind(p)
47. // bind 方法可以将函数中的this执行所指定的对象
48. </script>