ECMA

作业

作业一

  1. var plan = '做大餐';
  2. var week = window.prompt('请填写星期几');
  3. switch (week) {
  4. case '星期一':
  5. document.write(week);
  6. break;
  7. case '星期二':
  8. document.write(week);
  9. break;
  10. case '星期三':
  11. document.write(week);
  12. break;
  13. case '星期四':
  14. document.write(week);
  15. break;
  16. case '星期五':
  17. document.write(week);
  18. break;
  19. case '星期六':
  20. document.write(week);
  21. break;
  22. case '星期日':
  23. document.write(week);
  24. break;
  25. default:
  26. week = false;
  27. }
  28. var day = window.prompt('选择上午或者下午');
  29. switch (day) {
  30. case '上午':
  31. document.write(day);
  32. break;
  33. case '下午':
  34. document.write(day);
  35. break;
  36. default:
  37. day = false;
  38. }
  39. if (week && day) {
  40. document.write('的安排: ' + plan)
  41. } else {
  42. document.write('没有安排')
  43. }
  1. //日程安排
  2. var weekday = window.prompt('请输入星期几');
  3. var time = window.prompt('请输入上午或下午'); \
  4. switch (weekday) {
  5. case '星期一':
  6. if (time == '上午') {
  7. console.log('看书');
  8. } else if (time == '下午') {
  9. console.log('逛街');
  10. }
  11. break;
  12. }

作业二

for循环写斐波那契数列(黄金分割数列、兔子数列),

算出第n位

只知道两位,然后算出后面的值,前两位的和等于后一位

1 1 2 3 5 8…

  1. //循环一: n1 n2 n3
  2. // 1 1 2 3 5 8
  3. //循环二: n1 n2 n3
  4. //循环三: n1 n2 n3
  5. //循环四: n1 n2 n3
  6. //前两位是确定的
  7. //n3 = n1 + n2
  8. var n =parseInt(window.prompt('请输入第几位'));
  9. var n1 = 1,
  10. n2 = 1,
  11. n3;
  12. if (n < 0) {
  13. console.log('输入数字错误');
  14. } else {
  15. if (n <= 2) {
  16. n3 = 1;
  17. }
  18. for (var i = 2; i < n; i++){
  19. n3 = n1 + n2;
  20. n1 = n2;
  21. n2 = n3;
  22. }
  23. console.log(n3);
  24. }

作业三

定义一个函数,从wp接收一个饮料的名称,函数返回对应的价格(switch or if)

  1. function softDrinkPrice(name) {
  2. switch (name) {
  3. case '可乐':
  4. console.log('2.5元');
  5. break;
  6. case '雪碧':
  7. console.log('2.6元');
  8. break;
  9. default:
  10. console.log('请输入饮料名称');
  11. }
  12. }
  13. softDrinkPrice('可乐'); //2.5元
  14. softDrinkPrice('雪碧'); //2.5元
  15. softDrinkPrice('哈哈哈'); //请输入饮料名称

作业四

定义一个函数,从wp接收第一个数,接收一个运算符号(+,-,*,/,%),接收第二个数,利用函数做运算并返回运算结果

  1. function Cal(calSign, num1, num2) {
  2. switch (calSign) {
  3. case '+':
  4. console.log(num1 + num2);
  5. break;
  6. case '-':
  7. console.log(num1 - num2);
  8. break;
  9. case '*':
  10. console.log(num1 * num2);
  11. break;
  12. case '/':
  13. console.log(num1 / num2);
  14. break;
  15. case '%':
  16. console.log(num1 % num2);
  17. break;
  18. default:
  19. return console.log('请输入运算符号');
  20. }
  21. }
  22. // Cal('+', 1, 2); //3
  23. // Cal('-', 1, 2); //-1
  24. // Cal('*', 1, 2); //2
  25. // Cal('/', 1, 2); //0.5
  26. // Cal('%', 1, 2); //1

作业五

定义一个函数,从wp接收一个n,算出n的阶乘,不能用for循环

  1. //result = 1 x 2 x 3 x ... x n
  2. //规律 n = n * (n - 1)
  3. //出口
  4. function factor(num) {
  5. if (num < 0) {
  6. return -1;
  7. } else if (num === 0 || num === 1) {
  8. return 1;
  9. } else {
  10. return (num * factor(num - 1));
  11. }
  12. }
  13. console.log(factor(5)); //120
  14. //队列
  15. //fact(5) = 5 * fact(4);
  16. //fact(4) = 4 * fact(3);
  17. //fact(3) = 3 * fact(2);
  18. //fact(2) = 2 * fact(1);
  19. //fact(5) = 5 * 4;
  20. //fact(4) = 4 * 3;
  21. //fact(3) = 3 * 2;
  22. //fact(2) = 2 * 1;

定义一个函数,从wp接收一个n,算出斐波那契数列的第n位,不能用for循环

  1. //1 1 2 3 5 8 ...
  2. //规律:n3 = n2 + n1;
  3. //出口:n <= 2
  4. function fibo(n) {
  5. if (n == 1 || n == 2) {
  6. return 1
  7. };
  8. return fibo(n - 1) + fibo(n - 2);
  9. };
  10. console.log(fibo(6));
  11. n = 5;
  12. fb(5) = fb(4) + fb(3);
  13. fb(4) = fb(3) + fb(2);
  14. fb(3) = fb(2) + fb(1);
  15. fb(5) = 3 + 2;
  16. fb(4) = 2 + 1;
  17. fb(3) = 1 + 1;
  18. fb(5) = 5

作业六

写闭包累加器,执行一次打印一次

  1. function accumulator(num) {
  2. var num = arguments[0] || 1;
  3. function like() {
  4. num++;
  5. console.log(num);
  6. }
  7. function unlike() {
  8. num--;
  9. console.log(num);
  10. }
  11. return [like, unlike];
  12. }
  13. var result = accumulator();
  14. result[0]()
  15. result[0]()
  16. result[1]()

写一个缓存区,一个班级,学生名字保存在一个数组里,

两个方法写在函数的一个对象中,第一个方法是加入半接,第二个方法是离开班级,

每次加入或离开,都需要打印新的学生名单

  1. function classOne(name) {
  2. var sList = [];
  3. var operate = {
  4. enter: function () {
  5. console.log(name + '加入班级');
  6. sList += name;
  7. },
  8. leave: function () {
  9. console.log(name + '离开班级');
  10. sList -= name;
  11. }
  12. }
  13. return operate;
  14. }
  15. classOne('大田').enter();
  16. classOne('大田').leave();
  1. function myClass() {
  2. var sList = [];
  3. var operation = {
  4. join: function (name) {
  5. sList.push(name);
  6. console.log(sList);
  7. },
  8. leave: function (name) {
  9. for (var i = 0; i < sList.length; i++) {
  10. var item = sList[i];
  11. if (item === name) {
  12. sList.splice(i, 1);
  13. }
  14. }
  15. }
  16. }
  17. return operation;
  18. }
  19. var obj = myClass();
  20. obj.join('zhangsan');
  21. obj.join('lisi');
  22. obj.join('wangwu');
  23. obj.leave('lisi');

作业七

写一个构造函数,接收数字类型的参数,参数数量不定,完成参数相加和相乘的功能

  1. function MyMath(option) {
  2. this.number1 = option.number1;
  3. this.number2 = option.number2;
  4. this.add = function () {
  5. console.log(this.number1 + this.number2);
  6. }
  7. this.mul = function () {
  8. console.log(this.number1 * this.number2);
  9. }
  10. }
  11. var test = new MyMath({
  12. number1: 2,
  13. number2: 10
  14. });
  15. test.add();
  16. test.mul();
  1. function Compute() {
  2. var args = arguments,
  3. res;
  4. this.plus = function () {
  5. res = 0;
  6. loop('add', res);
  7. }
  8. this.times = function () {
  9. res = 1;
  10. loop('mul', res);
  11. }
  12. function loop(method, res) {
  13. for (var i = 0; i < args.length; i++) {
  14. var item = args[i];
  15. if (method === 'add') {
  16. res += item;
  17. } else if (method === 'mul') {
  18. res *= item;
  19. }
  20. }
  21. console.log(res);
  22. }
  23. }
  24. var compute = new Compute(2, 4, 6);
  25. compute.plus(); //12
  26. compute.times(); //48
  1. function Compute() {
  2. var res = 0;
  3. this.plus = function (args) {
  4. // console.log(arguments);
  5. loop(arguments, 'add', res);
  6. }
  7. this.times = function (args) {
  8. res = 1;
  9. loop(arguments, 'mul', res);
  10. }
  11. function loop(args, method, res) {
  12. for (var i = 0; i < args.length; i++) {
  13. var item = args[i];
  14. if (method === 'add') {
  15. res += item;
  16. } else if (method === 'mul') {
  17. res *= item;
  18. }
  19. }
  20. console.log(res);
  21. }
  22. }
  23. var compute = new Compute();
  24. compute.plus(2, 4, 6); //12
  25. compute.times(3, 5, 7); //48

作业八

写一个构造车的函数,可设置车的品牌,颜色,排量,再写一个构造消费者的函数,设置用户的名字,年龄,收入,通过选择的方法实例化该用户喜欢的车,再设置车的属性

  1. function Car(option) {
  2. this.brand = option.brand;
  3. this.color = option.color;
  4. this.displacement = option.displacement;
  5. }
  6. function User(option) {
  7. this.name = option.name;
  8. this.age = option.age;
  9. this.incoming = option.incoming;
  10. this.carSelect = option.carSelect;
  11. }
  12. var test = new MyMath({
  13. number1: 2,
  14. number2: 10
  15. });
  16. test.add();
  17. test.mul();
  1. function Car(color, brand, displacement) {
  2. this.brand = brand;
  3. this.color = color;
  4. this.displacement = displacement;
  5. this.drive = function () {
  6. console.log('I am driving');
  7. }
  8. }
  9. var car = new Car('red', 'mazda');
  10. console.log(car.color);
  11. console.log(car.brand);
  12. car.drive();
  1. function Car(opt) {
  2. this.brand = opt.brand;
  3. this.color = opt.color;
  4. this.displacement = opt.displacement;
  5. }
  6. function Person(opt) {
  7. this.name = opt.name;
  8. this.age = opt.age;
  9. this.income = opt.income;
  10. this.selectCar = function () {
  11. var myCar = new Car(opt.carOpt);
  12. console.log(this.name + '挑选了一辆排量为: ' + myCar.displacement + '的' + myCar.color + '的' + myCar.brand);
  13. }
  14. }
  15. var james = new Person({
  16. name: 'james',
  17. age: 18,
  18. income: '15k',
  19. carOpt: {
  20. brand: 'Mazda',
  21. color: '红色',
  22. displacement: '2.5L'
  23. }
  24. });
  25. james.selectCar();

作业九

写一个函数,接收任意一个字符串,算出这个字符串的总字节数

  1. function seachASCII(str) {
  2. var byte = 0;
  3. var result = 0;
  4. for (var i = 0; i < str.length; i++) {
  5. var pos = str.charCodeAt(i);
  6. if (pos < 255) {
  7. byte = 1;
  8. } else {
  9. byte = 2;
  10. }
  11. result += byte;
  12. }
  13. console.log(result);
  14. }
  15. seachASCII('abcdefa'); //7

写插件,任意传两个数字,调用插件内部方法可进行加减乘除功能

  1. var compute = (function () {
  2. function Compute() {
  3. var result = 0;
  4. switch (arguments[0].method) {
  5. case 'add':
  6. add(arguments[0]);
  7. break;
  8. case 'reduce':
  9. reduce(arguments[0]);
  10. break;
  11. case 'mul':
  12. mul(arguments[0]);
  13. break;
  14. case 'div':
  15. div(arguments[0]);
  16. break;
  17. default:
  18. console.log('请输入正确的运算符');
  19. }
  20. function add(arg) {
  21. result = arguments[0].a + arguments[0].b;
  22. console.log(result);
  23. }
  24. function reduce(arg) {
  25. result = arguments[0].a - arguments[0].b;
  26. console.log(result);
  27. }
  28. function mul(arg) {
  29. result = arguments[0].a * arguments[0].b;
  30. console.log(result);
  31. }
  32. function div(arg) {
  33. result = arguments[0].a / arguments[0].b;
  34. console.log(result);
  35. }
  36. }
  37. return Compute;
  38. })();
  39. compute({
  40. a: 1,
  41. b: 2,
  42. method: 'mul'
  43. });
  1. var compute = (function () {
  2. function Compute(opt) {
  3. this.a = opt.a || 0;
  4. this.b = opt.b || 0;
  5. this.method = opt.method;
  6. switch (this.method) {
  7. case 'add':
  8. Compute.prototype.add(this.a, this.b);
  9. break;
  10. case 'reduce':
  11. Compute.prototype.reduce(this.a, this.b);
  12. break;
  13. case 'mul':
  14. Compute.prototype.mul(this.a, this.b);
  15. break;
  16. case 'div':
  17. Compute.prototype.div(this.a, this.b);
  18. break;
  19. }
  20. }
  21. Compute.prototype = {
  22. add: function (a, b) {
  23. console.log(a + b);
  24. },
  25. reduce: function (a, b) {
  26. console.log(a - b);
  27. },
  28. mul: function (a, b) {
  29. console.log(a * b);
  30. },
  31. div: function (a, b) {
  32. console.log(a / b);
  33. }
  34. }
  35. return Compute;
  36. })();
  37. new compute({
  38. a: 1,
  39. b: 2,
  40. method: 'div'
  41. });

作业十

年龄为多少岁姓名为XX买了一辆排量为XX的XX颜色的XX牌子的车

  1. //硬性规定两个构造函数Car和Person合并
  2. function Car(displacement, color, brand) {
  3. this.displacement = displacement;
  4. this.color = color;
  5. this.brand = brand;
  6. }
  7. function Person(args) {
  8. var carLike = args.carLike;
  9. Car.apply(this, carLike);
  10. this.name = args.name;
  11. this.age = args.age;
  12. this.carSelect = function () {
  13. console.log('年龄为' + this.age + '岁' + '姓名为' + this.name + '买了一辆排量为' + this.displacement + '的' + this.color + '颜色的' + this.brand + '牌子的车');
  14. }
  15. }
  16. var person = new Person({
  17. name: 'kevin',
  18. age: 30,
  19. carLike: ['2.5L', 'grey', 'Mazda']
  20. });
  21. person.carSelect();

写模块化:

  • 功能一:打印一个参数值以内能被3或5或7整除的数值
  • 功能二:打印斐波那契数列的第N位
  • 功能三:打印从0到100的累加值
  1. window.onload = function () {
  2. init();
  3. }
  4. function init() {
  5. initComputeDiv(10);
  6. initComputeFibo(10);
  7. initComputeAcc(10);
  8. }
  9. var initComputeDiv = (function () {
  10. //打印一个参数值以内能被3或5或7整除的数值
  11. function div(num) {
  12. var arr = [];
  13. for (var i = 0; i < num; i++) {
  14. if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0) {
  15. arr.push(i);
  16. }
  17. }
  18. console.log(arr);
  19. return this;
  20. }
  21. return div;
  22. })();
  23. var initComputeFibo = (function () {
  24. //打印斐波那契数列的第N位
  25. function fibo(n) {
  26. var n1 = 1,
  27. n2 = 1,
  28. n3;
  29. if (n < 0) {
  30. console.log('数字错误');
  31. } else {
  32. if (n <= 2) {
  33. n3 = 1;
  34. }
  35. for (var i = 2; i < n; i++) {
  36. n3 = n1 + n2;
  37. n1 = n2;
  38. n2 = n3;
  39. }
  40. console.log(n3);
  41. }
  42. }
  43. return fibo;
  44. })();
  45. var initComputeAcc = (function () {
  46. //打印从0到100的累加值
  47. function sum(num) {
  48. var result = 0;
  49. for (var i = 0; i <= num; i++) {
  50. result += i;
  51. }
  52. console.log(result);
  53. }
  54. return sum;
  55. })();
  1. //请用window.prompt接收用户输入的年份,判断是否是闰年?(三目运算符)
  2. //整除4 并且不能整除100
  3. //整除400
  4. var year = window.prompt('请输入年份');
  5. (year % 4 == 0 && year % 100 !== 0) || year % 400 === 0 ? console.log('是闰年') : console.log('不是闰年');

作业十一

splice()重写原型数组的原型上的unshift()方法myUnshift()

  1. var arr = ['d', 'e', 'f'];
  2. //['a', 'b', 'c', 'd', 'e', 'f']
  3. Array.prototype.myUnshift = function () {
  4. //unshift 在第一位前面加
  5. //返回值都为执行了方法以后数组长度
  6. //传入实参列表
  7. for (var i = arguments.length - 1; i >= 0; i--) {
  8. // console.log(arguments[i]); //c b a
  9. //arr.splice(开始项的下标,剪切长度,剪切以后最后一位开始添加数据)
  10. this.splice(0, 0, arguments[i]);
  11. }
  12. //功能2:返回执行方法以后数组长度
  13. return this.length;
  14. }
  15. arr.myUnshift('a', 'b', 'c');
  16. console.log(arr);
  1. Array.prototype.myUnshift = function () {
  2. var pos = 0;
  3. for (var i = 0; i < arguments.length; i++){
  4. // console.log(arguments[i]);//a b c
  5. this.splice(pos, 0, arguments[i]);
  6. pos++;
  7. }
  8. return this.length;
  9. }
  10. arr.myUnshift('a', 'b', 'c');
  11. console.log(arr);
  1. //利用concat()数组拼接
  2. Array.prototype.myUnshift = function () {
  3. //先让类数组转为数组
  4. var argArr = Array.prototype.slice.call(arguments);
  5. // console.log(argArr); //["a", "b", "c"]
  6. //返回值:修改原数组
  7. arr = argArr.concat(this);
  8. return arr.length;
  9. }
  10. arr.myUnshift('a', 'b', 'c'); //["a", "b", "c", "d", "e", "f"]
  11. // arr.myUnshift(1); //[1, "d", "e", "f"]
  12. console.log(arr);

请按照字节数排序下列数组,charCodeAt() > 255

['我爱你', 'OK', 'Hello', '你说WHAT', '可以']

  1. // //请按照字节数排序下列数组
  2. var arr = ['我爱你', 'OK', 'Hello', '你说WHAT', '可以'];
  3. var newArr = [];
  4. for (var i = 0; i < arr.length; i++) {
  5. // console.log(i); // console.log(i); 0 1 2 3 4
  6. // console.log(arr[i]); //我爱你 OK Hello 你说WHAT 可以
  7. // console.log(arr[0]); //我爱你
  8. var res = seachASCII(arr[i]); //6 2 5 8 4
  9. //{
  10. // 6: '我爱你',
  11. // 2: 'OK'
  12. //}
  13. var obj = {};
  14. //对象键名 = 对象键值
  15. obj[res] = arr[i];
  16. // console.log(obj);
  17. /**
  18. * {6: "我爱你"}
  19. * {2: "OK"}
  20. * {5: "Hello"}
  21. * {8: "你说WHAT"}
  22. * {4: "可以"}
  23. */
  24. newArr.push(obj);
  25. }
  26. newArr.sort(function (a, b) {
  27. if (a[res] > b[res]) {
  28. return 1;
  29. } else {
  30. return -1;
  31. }
  32. })
  33. console.log(newArr);
  34. function seachASCII(str) {
  35. var byte = 0;
  36. var result = 0;
  37. for (var i = 0; i < str.length; i++) {
  38. var pos = str.charCodeAt(i);
  39. if (pos < 255) {
  40. byte = 1;
  41. } else {
  42. byte = 2;
  43. }
  44. result += byte;
  45. }
  46. return result;
  47. }
  48. // var arr = [];
  49. // for (var i = 0; i < 5; i++) {
  50. // arr.push(i);
  51. // };
  52. // console.log(arr);
  1. //数组按照元素的字节数排序
  2. var arr = ['我爱你', 'OK', 'Hello', '你说WHAT', '可以'];
  3. arr.sort(function (a, b) {
  4. //比较函数获取到的字符即可
  5. return getBytes(a) - getBytes(b);
  6. })
  7. //unicode 0-255 -> 1个字节
  8. //unicode > 255 -> 2个字节
  9. function getBytes(str) {
  10. //我爱你 -> bytes = 3
  11. //不管中文还是英文都当成一个字节
  12. //让下面判断的时候再决定是否加一个字节
  13. var bytes = str.length;
  14. for (var i = 0; i < str.length; i++){
  15. //逐个查询字符遇到>255的加一个字节
  16. if (str.charCodeAt(i) > 255) {
  17. bytes++;
  18. }
  19. }
  20. return bytes;
  21. }
  22. console.log(getBytes('我爱你')); //6
  23. console.log(arr); //["OK", "可以", "Hello", "我爱你", "你说WHAT"]
  1. //数组去重
  2. //Array.protype原型上写unique方法
  3. //实现数据去重 返回新的数组
  4. //重复的项目剔除
  5. //可以用对象属性的方式
  6. //把属性或者值拿出来重新做一个数组
  7. var arr = [0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 'a', 'a'];
  8. Array.prototype.unique = function () {
  9. var temp = {},
  10. newArr = [];
  11. for (var i = 0; i < this.length; i++) {
  12. // console.log(this[i]); //0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 'a', 'a'
  13. //写法一:
  14. // if (!temp[this[i]]) {
  15. // //temp对象属性 = arr对象属性
  16. // // temp[this[i]] = this[i];
  17. // //过滤0改写为'1111'
  18. // temp[this[i]] = '1111';
  19. // // console.log(temp);
  20. // //{0: "1111", 1: "1111", 2: "1111", 3: "1111", a: "1111"}
  21. // newArr.push(this[i]);
  22. // }
  23. //写法二:
  24. if (!temp.hasOwnProperty(this[i])) {
  25. //temp对象属性 = arr对象属性
  26. temp[this[i]] = this[i];
  27. console.log(temp);
  28. //{0: 0, 1: 1, 2: 2, 3: 3, a: "a"}
  29. newArr.push(this[i]);
  30. }
  31. }
  32. return newArr;
  33. }
  34. //这里发现0没过滤掉 因为!temp[this[i] -> !0 -> true
  35. console.log(arr.unique()); //[0, 0, 1, 2, 3, "a"]
  36. console.log(arr.unique()); //[0, 1, 2, 3, "a"]
  37. //原理分析:
  38. // //借用对象不重复性的属性
  39. // //根据数组判断自身有没有该属性
  40. // //如果没有就给该对象添加属性
  41. // var obj = {
  42. // 0: 0,
  43. // 1: 1,
  44. // 2: 2
  45. // }
  46. // //给对象放属性的同时给数组也添加元素
  47. // var newArr = [];
  1. //字符串去重
  2. var str = '111222000aabb';
  3. String.prototype.unique = function () {
  4. var temp = {},
  5. newStr = '';
  6. for (var i = 0; i < this.length; i++) {
  7. if (!temp.hasOwnProperty(this[i])) {
  8. temp[this[i]] = this[i];
  9. newStr += this[i];
  10. }
  11. }
  12. return newStr;
  13. }
  14. console.log(str.unique()); //120ab
  1. //找出不重复的字符串并返回次数
  2. var str = 'truaiwrtruibowrtpoiwrcutwopirut';
  3. function test(str) {
  4. var temp = {};
  5. for (var i = 0; i < str.length; i++) {
  6. if (temp.hasOwnProperty(str[i])) {
  7. // console.log(temp[str[i]]);
  8. temp[str[i]]++;
  9. } else {
  10. temp[str[i]] = 1;
  11. }
  12. }
  13. return temp;
  14. }
  15. console.log(test(str));
  16. /**
  17. * {t: 5, r: 6, u: 4, a: 1, i: 4, …}
  18. a: 1
  19. b: 1
  20. c: 1
  21. i: 4
  22. o: 3
  23. p: 2
  24. r: 6
  25. t: 5
  26. u: 4
  27. w: 4
  28. __proto__: Object
  29. */
  1. //封装myTypeof方法
  2. //Object.prototype.toString.call();
  3. //原始值:
  4. //typeof('123') -> 'string';
  5. //typeof(123) -> 'number';
  6. //typeof(null) -> 'object';
  7. //typeof(NaN) -> 'number';
  8. //typeof(undefined) -> 'undefined';
  9. //typeof(true) -> 'boolean';
  10. //toString.call('123') -> '[object String]'
  11. //toString.call(123) -> '[object Number]'
  12. //toString.call(null) -> '[object Null]'
  13. //toString.call(NaN) -> '[object Number]'
  14. //toString.call(undefined) -> '[object Undefined]'
  15. //toString.call(true) -> '[object Boolean]'
  16. //引用值
  17. //typeof({}) -> 'object';
  18. //typeof([]) -> 'object';
  19. //typeof(function(){}) -> 'function';
  20. //toString.call({}) -> '[object Object]'
  21. //toString.call([]) -> '[object Array]'
  22. //toString.call(function(){}) -> '[object Function]'
  23. //包装类
  24. //typeof(new Number(1)) -> 'object';
  25. //typeof(new String('1')) -> 'object';
  26. //typeof(new Boolean(true)) -> 'object';
  27. //toString.call(new Number(1)) -> '[object Number]'
  28. //toString.call(new String('123')) -> '[object String]'
  29. //toString.call(new Boolean(true)) -> '[object Boolean]'
  30. function myTypeof(val) {
  31. var toStr = Object.prototype.toString,
  32. type = typeof (val),
  33. res = toStr.call(val);
  34. //包装类和 引用值 null {} []
  35. if (type === 'object') {
  36. switch (res) {
  37. case '[object Null]':
  38. type = 'null';
  39. break;
  40. case '[object Object]':
  41. type = 'object';
  42. break;
  43. case '[object Array]':
  44. type = 'array';
  45. break;
  46. case '[object Number]':
  47. type = 'object number';
  48. break;
  49. case '[object String]':
  50. type = 'object string';
  51. break;
  52. case '[object Boolean]':
  53. type = 'object boolean';
  54. break;
  55. }
  56. }
  57. return type;
  58. }
  59. //原始值
  60. console.log(myTypeof('123')); //'string'
  61. console.log(myTypeof(123)); //'number'
  62. console.log(myTypeof(null)); //'null'
  63. console.log(myTypeof(NaN)); //'number'
  64. console.log(myTypeof(undefined)); //'undefined'
  65. console.log(myTypeof(true)); //'boolean'
  66. //引用值
  67. console.log(myTypeof({})); //'object'
  68. console.log(myTypeof([])); //'array'
  69. console.log(myTypeof(function () {})); //'function'
  70. //包装类
  71. console.log(myTypeof(new Number(1))); //object number
  72. console.log(myTypeof(new String('123'))); //object string
  73. console.log(myTypeof(new Boolean(true))); //object boolean
  1. //封装myTypeof()
  2. function myTypeof(val) {
  3. var type = typeof (val),
  4. toStr = Object.prototype.toString,
  5. res = {
  6. '[object Array]': 'array',
  7. '[object Object]': 'object',
  8. '[object Number]': 'object number',
  9. '[object String]': 'object string',
  10. '[object Boolean]': 'object boolean'
  11. }
  12. if (val === null){
  13. return 'null';
  14. //object: null {} []
  15. //这里返回包括包装类Number, String, Boolean
  16. } else if (type === 'object') {
  17. var ret = toStr.call(val);
  18. return res[ret];
  19. //返回原始值
  20. //
  21. } else {
  22. return type;
  23. }
  24. }

dom:

作业一:

  1. //作业1
  2. //在原型上编程
  3. //遍历任意一个父元素,找到他的子元素节点
  4. //有数字参数 返回 某一个对应子元素
  5. //没有数字参数 返回 子元素节点的集合
  6. Document.prototype.findChildEleNode = function (fNode, num) {
  7. var fNode = arguments[0],
  8. sNode = fNode.childNodes,
  9. childEleSet = [],
  10. typeNum = arguments[1] || 0;
  11. // console.log(sNode);
  12. for (var i = 0; i < sNode.length; i++) {
  13. var nodeType = sNode[i].nodeType,
  14. childItem = sNode[i];
  15. // 有参数
  16. if (nodeType === typeNum) {
  17. childEleSet.push(childItem);
  18. } else {
  19. //没有参数
  20. // console.log(sNode);
  21. // childEleSet = sNode;
  22. }
  23. }
  24. return childEleSet;
  25. }
  26. console.log(document.findChildEleNode(div, 9));;

作业二

  1. //作业2
  2. //在原型上编程
  3. //找出一个元素的第n层父级元素
  4. var a = document.getElementsByTagName('a')[0];
  5. Document.prototype.findFatherTagName = function (ele, n) {
  6. console.log(ele.parentNode.parentNode);
  7. var fatherEle = ele;
  8. for (var i = 1; i <= n; i++) {
  9. fatherEle = ele.parentNode;
  10. }
  11. return fatherEle;
  12. }
  13. document.findFatherTagName(a, 1)

作业三

  1. //作业3:原型上编程hasChildren
  2. //判断是否有子节点(包括文本节点)
  3. //判断父元素有没有子元素节点
  4. // parent.hasChildNodes
  5. // console.log(div.hasChildNodes()); //true
  6. Node.prototype.hasChildNodes = function () {
  7. var children = this.childNodes,
  8. eleNode = [],
  9. result = false;
  10. for (var i = 0; i < children.length; i++) {
  11. var item = children[i];
  12. if (item.nodeType === 1) {
  13. eleNode.push(item);
  14. // console.log(eleNode);
  15. if (eleNode) {
  16. result = true;
  17. } else {
  18. result = false;
  19. }
  20. }
  21. }
  22. return result;
  23. }
  24. console.log(div.hasChildNodes());

作业四

  1. //作业4:原型上编程(未做完)
  2. //寻找兄弟元素节点
  3. //参数为正:找之后的第N个
  4. //参数为负:找之前的第N个
  5. Node.prototype.findEleSibling = function (num) {
  6. // console.log(this.nodeType);
  7. //拿到同级所有的节点
  8. var elem = this.parentNode.childNodes,
  9. elemList = [];
  10. //过滤除元素节点之外的节点
  11. for (var i = 0; i < elem.length; i++) {
  12. var item = elem[i];
  13. if (elem[i].nodeType === 1) {
  14. elemList.push(item);
  15. }
  16. }
  17. //找回原来位置 ????
  18. console.log(elemList[0]);
  19. // if (num > 0) {
  20. // } else if (num < 0) {
  21. // num = Math.abs(num);
  22. // // console.log(num);
  23. // }
  24. //负数
  25. //参数1: console.log(this.previousSibling.previousSibling);
  26. //参数2: console.log(this.previousSibling.previousSibling.previousSibling.previousSibling);
  27. //正数
  28. //参数1:
  29. // console.log(this.nextSibling.nextSibling);
  30. //参数2: console.log(this.nextSibling.nextSibling.nextSibling.nextSibling)
  31. }
  32. div.findEleSibling(1);
  1. //作业5: JS创建HTML结构
  2. //div > ul > li class="list item" x 5
  3. var div = document.createElement('div'),
  4. oList = document.createElement('ul'),
  5. oFrag = document.createDocumentFragment();
  6. for (var i = 1; i <= 5; i++) {
  7. var oLi = document.createElement('li');
  8. oLi.innerHTML = i;
  9. oLi.className = 'list-item';
  10. oFrag.appendChild(oLi);
  11. }
  12. oList.appendChild(oFrag);
  13. div.appendChild(oList);
  14. console.log(div);
  1. //作业6:遍历一个父级元素下面所有的子元素节点(未完成)
  2. //递归
  3. Node.prototype.findAllElem = function () {
  4. console.log(this);
  5. // console.log(this.childNodes[1].childNodes[1]);
  6. var elemList = [];
  7. for (var i = 0; i < this.childNodes.length; i++) {
  8. if (this.childNodes[i].nodeType === 1) {
  9. elemList.push(this.childNodes[i]);
  10. console.log(elemList);
  11. }
  12. }
  13. }
  14. div.findAllElem();
  1. function elemChildren(node) {
  2. var temp = {
  3. 'length': 0,
  4. 'push': Array.prototype.push,
  5. 'splice': Array.prototype.splice
  6. };
  7. var children = node.childNodes,
  8. len = children.length,
  9. item;
  10. for (var i = 0; i < len; i++) {
  11. item = children[i];
  12. if (item.nodeType === 1) {
  13. temp.push(item);
  14. }
  15. }
  16. return temp;
  17. }
  18. console.log(elemChildren(box)[1]);

事件流

作业1:

做列表 点击增加li 按钮

  1. 清空input框内容
  2. list列表后增加项
  1. <div>
  2. <input type="text" id="add" />
  3. <button class="addBtn">增加li</button>
  4. <ul class="list">
  5. </ul>
  6. </div>
  1. var oList = document.getElementsByClassName('list')[0],
  2. oInput = document.getElementById('add'),
  3. oBtn = document.getElementsByClassName('addBtn')[0];
  4. oBtn.addEventListener('click', function (e) {
  5. var e = e || window.event,
  6. tar = e.target || e.srcElement,
  7. li = document.createElement('li');
  8. li.innerHTML = oInput.value;
  9. oList.appendChild(li);
  10. oInput.value = '';
  11. }, false);

作业2:todolist

  1. <div>
  2. <input type="text" id="add" />
  3. <button class="addBtn">增加li</button>
  4. <ul class="list">
  5. </ul>
  6. </div>
  1. // 作业1
  2. // 做列表 点击增加li 按钮
  3. //新增:
  4. //1.清空input框内容
  5. //2.list列表后增加项
  6. //编辑:
  7. //1.点击某一项把某项数据填到输入框文本域
  8. //2.点击按钮文本内容改为 编辑到第几项
  9. //3.点击按钮成功修改该项信息
  10. var oList = document.getElementsByClassName('list')[0],
  11. oInput = document.getElementById('add'),
  12. oBtn = document.getElementsByClassName('addBtn')[0],
  13. isEdit = false,
  14. inputValue = '',
  15. idx;
  16. //动态创建的ul
  17. var oItems = document.getElementsByClassName('item');
  18. function init() {
  19. bindEvent();
  20. }
  21. //事件绑定
  22. function bindEvent() {
  23. //按钮事件
  24. addEvent(oBtn, 'click', btnClick);
  25. //列表事件:修改
  26. addEvent(oList, 'click', edit);
  27. //列表事件:删除
  28. addEvent(oList, 'click', del);
  29. }
  30. function btnClick(e) {
  31. //情况1:输入框为空不能点按钮
  32. if (oInput.value.length === 0) {
  33. return;
  34. }
  35. //去重
  36. var _item;
  37. //遍历对比li里面的text
  38. for (var i = 0; i < oItems.length; i++) {
  39. _item = oItems[i].innerText.split(' ')[0];
  40. if (oInput.value === _item) {
  41. alert('已存在该项目');
  42. return;
  43. }
  44. }
  45. // 新增/编辑
  46. if (isEdit) {
  47. //编辑
  48. oItems[idx].innerHTML = tpl(oInput.value);
  49. reset();
  50. isEdit = false;
  51. } else {
  52. //新增
  53. add();
  54. }
  55. }
  56. // 新增操作
  57. function add() {
  58. //增加操作: 1.获取输入框的文本值并保存至全局
  59. inputValue = oInput.value;
  60. //增加操作: 2.创建新的li节点并新增类名
  61. var oLi = document.createElement('li');
  62. oLi.className = 'item';
  63. //增加操作: 3.把处理好的字符串赋值到Li的文本里
  64. oLi.innerHTML = tpl(inputValue);
  65. //增加操作: 4.将处理好的li逐条加入到ul里
  66. oList.appendChild(oLi);
  67. //重置输入框内容
  68. reset();
  69. isEdit = false;
  70. }
  71. //修改操作
  72. function edit(e) {
  73. var e = e || window.event,
  74. tar = e.target || e.srcElemnt;
  75. //点击到编辑时才能进行下面操作:
  76. if (tar.innerText === ' 编辑') {
  77. //修改操作: 1.拿到被点击的当前li项
  78. var oLi = elemParent(tar, 1),
  79. //修改操作: 2.拿到被点击的当前li项里面的文本内容
  80. text = oLi.innerText.split(' ')[0];
  81. //修改操作: 3.将文本内容放到文本输入框里
  82. oInput.value = text;
  83. //修改操作: 4.找到该项索引值并修改按钮文本信息
  84. idx = Array.prototype.indexOf.call(oItems, oLi);
  85. oBtn.innerText = '修改第' + (idx + 1) + '项';
  86. //激活可编辑状态
  87. isEdit = true;
  88. console.log('编辑状态:启用');
  89. }
  90. }
  91. function del(e) {
  92. var e = e || window.event,
  93. tar = e.target || e.srcElemnt;
  94. //点击到编辑时才能进行下面操作:
  95. if (tar.innerText === ' 删除') {
  96. //找到当前项
  97. var oLi = elemParent(tar, 1);
  98. //删除当前项
  99. elemParent(oLi, 1).removeChild(oLi);
  100. }
  101. }
  102. function tpl(text) {
  103. return (
  104. text +
  105. '<a href="javascript:;" style="text-decoration: none;"> 编辑</a>' +
  106. '<a href="javascript:;" style="text-decoration: none;"> 删除</a>'
  107. )
  108. }
  109. function reset() {
  110. oInput.value = '';
  111. oBtn.innerText = '增加li'
  112. }
  113. function findNodeElem(elem, type) {
  114. var _arrLike = {
  115. 'length': 0,
  116. 'push': Array.prototype.push,
  117. 'slice': Array.prototype.slice
  118. },
  119. children = elem.childNodes,
  120. item;
  121. for (var i = 0; i < children.length; i++) {
  122. item = children[i];
  123. if (item.nodeType === 1) {
  124. _arrLike.push(item);
  125. }
  126. }
  127. return _arrLike;
  128. }
  129. /**
  130. * 封装兼容低版本的事件绑定处理函数
  131. * @el 元素
  132. * @type 事件类型
  133. * @fn 事件处理函数
  134. */
  135. function addEvent(el, type, fn) {
  136. if (el.addEventListener) {
  137. el.addEventListener(type, fn, false);
  138. } else if (el.attachEvent) {
  139. el.attachEvent('on' + type, function () {
  140. fn.call(el);
  141. })
  142. } else {
  143. el['on' + type] = fn;
  144. }
  145. }
  146. /**
  147. * 封装找父级元素的函数
  148. * @node 子元素节点
  149. * @n 寻找第几个父级元素
  150. * @返回值 返回父级元素
  151. */
  152. function elemParent(node, n) {
  153. var type = typeof (n);
  154. if (type === 'undefined') {
  155. return node.parentNode;
  156. } else if (n <= 0 || type !== 'number') {
  157. return undefined;
  158. }
  159. while (n) {
  160. node = node.parentNode;
  161. n--;
  162. }
  163. return node;
  164. }
  165. init();

面试题

  1. var a = false + 1;
  2. console.log(a); //1
  1. var b = false == 1;
  2. console.log(b); //false
  1. //'undefined' -> true
  2. //undefined -> false
  3. //typeof (a) -> typeof (undefined) -> 'undefined' -> true
  4. //(-true) -> -1
  5. //(+undefined) -> Number(undefined) -> NaN
  6. //(-1 + NaN + '') -> 'NaN' -> true
  7. //true && true -> true -> console.log('通过了')
  8. if (typeof (a) && (-true) + (+undefined) + '') {
  9. console.log('通过了');
  10. } else {
  11. console.log('没通过');
  12. }
  1. //1 + 15 = 16 == 16 -> true
  2. if (1 + 5 * '3' == 16) {
  3. console.log('通过了');
  4. } else {
  5. console.log('未通过');
  6. }
  7. //16(number) => true
  8. if (1 + 5 * '3' === 16) {
  9. console.log('通过了');
  10. } else {
  11. console.log('未通过');
  12. }
  1. //' ' = false -> !! -> true
  2. //'' = false -> !! -> flase
  3. //!!false -> false
  4. //true + false - false = 1
  5. // 1 || '通过了' -> 1
  6. console.log(!!' ' + !!'' - !!false || '通过了'); //1
  1. //(a, b) 只返回逗号后面
  2. var fn = (
  3. function test1() {
  4. return 1;
  5. },
  6. function test1() {
  7. return '2';
  8. }
  9. )();
  10. console.log(typeof (fn)); //string
  1. //(function b() {})为表达式忽略b所以没有b,所以为undefined
  2. //console.log(b)报错,但console.log(typeof (b))报undefined
  3. //typeof (undefined) => undefined
  4. var a = 10;
  5. if (function b() {}) {
  6. a += typeof (b);
  7. }
  8. console.log(a); //10undefined
  1. //面试题
  2. function Foo() {
  3. getName = function () {
  4. console.log('111');
  5. }
  6. return this;
  7. }
  8. Foo.getName = function () {
  9. console.log('222');
  10. }
  11. Foo.prototype.getName = function () {
  12. console.log('333');
  13. }
  14. var getName = function () {
  15. console.log('444');
  16. }
  17. function getName() {
  18. console.log('555');
  19. }
  20. //输出结果
  21. Foo.getName(); //222
  22. getName(); //function getName(){console.log(444);}
  23. Foo().getName(); //111
  24. getName(); //111
  25. new Foo.getName(); //222
  26. new Foo().getName(); //333
  27. new new Foo().getName(); //333
  28. //解题过程
  29. /**
  30. * GO = {
  31. * getName: undefined
  32. * -> function getName(){console.log(555);}
  33. * -> function getName(){console.log(444);}
  34. * //执行Foo()
  35. * -> function getName(){console.log(111);}
  36. * }
  37. */
  1. //最先想到的办法:for循环
  2. function uniqueArr(array) {
  3. //数组容器
  4. var _arr = [],
  5. isReapeat;
  6. for (var i = 0; i < array.length; i++) {
  7. //默认为false
  8. isReapeat = false;
  9. //遍历数组容器
  10. for (var j = 0; j < _arr.length; j++) {
  11. //证明重复了
  12. if (_arr[j] == array[i]) {
  13. //并定义为true
  14. isReapeat = true;
  15. //不能往_arr容器里增加元素
  16. break;
  17. }
  18. }
  19. //这里外层for循环仍会继续循环
  20. //如需终止循环,声明变量isReapeat枷锁
  21. if (!isReapeat) {
  22. //在循环外push元素
  23. _arr.push(array[i]);
  24. }
  25. }
  26. return _arr;
  27. }
  28. console.log(uniqueArr(arr).sort());;
  29. //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

笔试题

  1. /**
  2. *
  3. AO = {
  4. a: undefined -> function a() {}
  5. }
  6. */
  7. function test() {
  8. return a;
  9. a = 1;
  10. function a() {}
  11. var a = 2;
  12. }
  13. console.log(test()); //function a() {}
  1. /**
  2. * GO = {
  3. * a: 1,
  4. * test: function test(){...}
  5. }
  6. AO = {
  7. a: undefined -> function a() {} -> 2
  8. }
  9. */
  10. console.log(test()); //2
  11. function test() {
  12. a = 1;
  13. function a() {}
  14. var a = 2;
  15. return a;
  16. }
  1. /**
  2. * GO = {
  3. * a: undefined -> 1,
  4. test: function test(e){...},
  5. f: 5
  6. }
  7. AO = {
  8. a: undefined -> 4,
  9. b: undefined,
  10. c: undefined,
  11. e: undefined -> 1 -> function e() {} -> 2
  12. }
  13. */
  14. a = 1;
  15. function test(e) {
  16. function e() {}
  17. arguments[0] = 2;
  18. console.log(e); //2
  19. //a: undefined -> false
  20. if (a) {
  21. var b = 3;
  22. console.log(b); //不打印
  23. }
  24. var c;
  25. a = 4;
  26. var a;
  27. console.log(b); //undefined
  28. f = 5;
  29. console.log(c); //undefined
  30. console.log(a); //4
  31. }
  32. var a;
  33. test(1);
  34. console.log(a); //1
  35. console.log(f); //5
  1. function Test(a, b, c) {
  2. var d = 1;
  3. this.a = a;
  4. this.b = b;
  5. this.c = c;
  6. function f() {
  7. d++;
  8. console.log(d);
  9. }
  10. this.g = f;
  11. }
  12. var test1 = new Test();
  13. test1.g(); //2
  14. test1.g(); //3
  15. var test2 = new Test();
  16. test2.g(); //2
  1. //x = 1
  2. //y = 0
  3. //z = 0
  4. /**
  5. * GO = {
  6. * x: undefined -> 1,
  7. * y: undefined -> 0,
  8. * z: undefined -> 0,
  9. * add: function add(n){return n = n + 1;}
  10. * -> function add(n){return n = n + 3;}
  11. * }
  12. */
  13. var x = 1,
  14. y = z = 0;
  15. function add(n) {
  16. return n = n + 1;
  17. }
  18. y = add(x); //y = 4
  19. function add(n) {
  20. return n = n + 3;
  21. }
  22. z = add(x); //z = 4
  23. console.log(x, y, z); //1 4 4
  1. function foo1(x) {
  2. console.log(arguments);
  3. return x;
  4. }
  5. foo1(1, 2, 3, 4, 5); //可输出
  6. function foo2(x) {
  7. console.log(arguments);
  8. return x;
  9. }(1, 2, 3, 4, 5); //不可输出
  10. (function foo3(x) {
  11. console.log(arguments);
  12. return x;
  13. })(1, 2, 3, 4, 5); //可输出
  1. function b(x, y, a) {
  2. a = 10;
  3. console.log(arguments[2]); //10
  4. }
  5. b(1, 2, 3);
  6. //映射关系
  7. function b(x, y, a) {
  8. arguments[2] = 10;
  9. console.log(a); //10
  10. }
  11. b(1, 2, 3);
  1. //alibaba
  2. //console.log(arguments) => 空数组
  3. //bar() -> bar.call() -> bar(arguments)
  4. function foo() {
  5. bar.apply(null, arguments);
  6. //bar.call(arguments) -> bar(arguments) -> [1,2,3,4,5]
  7. }
  8. function bar() {
  9. console.log(arguments); //1,2,3,4,5
  10. }
  11. foo(1, 2, 3, 4, 5);
  1. //alibaba
  2. function b(x, y, a) {
  3. arguments[2] = 10;
  4. alert(a); //10
  5. }
  6. b(1, 2, 3);
  1. var f = (function f() {
  2. return '1';
  3. }, function g() {
  4. return 2;
  5. })
  6. console.log(typeof f); //function
  7. var f = (function f() {
  8. return '1';
  9. }, function g() {
  10. return 2;
  11. })()
  12. console.log(typeof f); //number
  1. //undefined > 0 -> false
  2. //undefined < 0 -> false
  3. //undefined = 0 -> false
  4. //null > 0 -> false
  5. //null < 0 -> false
  6. //null = 0 -> false
  7. //undefined == null -> true
  8. //undefined === null -> false
  9. //isNaN('100') -> false
  10. //isNaN('abc') -> true
  11. //var num = Number('100') -> isNaN(num) -> false
  12. //parseInt('123a') -> 123
  13. //parseInt('1a') -> 1
  14. console.log(undefined == null); //true
  15. console.log(undefined === null); //false
  16. console.log(isNaN('100')); //false
  17. console.log(parseInt('1a') == 1); //true
  18. //写一个isNaN()方法
  19. function isNaN1(num) {
  20. //由于NaN谁也不等于所有把它变为'NaN'
  21. var res = Number(num) + '';
  22. if (res == 'NaN') {
  23. return true;
  24. } else {
  25. return false;
  26. }
  27. }
  28. isNaN1('abc'); //NaN
  29. console.log(isNaN1('abc')); //true
  30. console.log(isNaN1(123)); //false
  1. {} = {} ?
  2. //为什么不等于?
  3. //因为引用值地址不一样
  4. //如何相等?
  5. var obj = {}
  6. obj1 = obj;
  7. console.log(obj == obj1); //true
  1. var a = '1';
  2. function test() {
  3. var a = '2';
  4. this.a = '3';
  5. console.log(a); //2 //2
  6. }
  7. test();
  8. new test();
  9. console.log(a); //3
  1. var a = 5;
  2. function test() {
  3. a = 0;
  4. console.log(a);
  5. console.log(this.a);
  6. var a;
  7. console.log(a);
  8. }
  9. test(); //0 5 0
  10. new test(); //0 undefined 0
  1. function test() {
  2. console.log(foo); //undefined
  3. var foo = 2;
  4. console.log(foo); //2
  5. console.log(a); //报错
  6. }
  7. test();
  1. function a() {
  2. var test;
  3. test();
  4. function test() {
  5. console.log(1); //1
  6. }
  7. }
  8. a();
  9. //test: undefined -> fuction test(){}
  1. //alibaba
  2. function test() {
  3. var marty = {
  4. name: 'marty',
  5. printName: function () {
  6. console.log(this.name);
  7. }
  8. }
  9. var test1 = {
  10. name: 'test1'
  11. }
  12. var test2 = {
  13. name: 'test2'
  14. }
  15. var test3 = {
  16. name: 'test3'
  17. }
  18. test3.printName = marty.printName;
  19. marty.printName.call(test1); //test1
  20. marty.printName.apply(test2); //test2
  21. marty.printName(); //marty
  22. test3.printName(); //test3
  23. }
  24. test()
  1. var bar = {
  2. a: '1'
  3. };
  4. function test() {
  5. bar.a = 'a'; //这里覆盖全局的值
  6. Object.prototype.b = 'b';
  7. return function inner() {
  8. console.log(bar.a); //'a'
  9. console.log(bar.b); //'b'
  10. }
  11. }
  12. test()();
  13. //此处test()() -> var test = test(); -> test();
  1. //如何优化
  2. function test(day) {
  3. switch (day) {
  4. case 1:
  5. console.log('Mon');
  6. break;
  7. case 2:
  8. console.log('Tue');
  9. break;
  10. case 3:
  11. console.log('Wed');
  12. break;
  13. case 4:
  14. console.log('Thur');
  15. break;
  16. case 5:
  17. console.log('Fri');
  18. break;
  19. case 6:
  20. console.log('Sat');
  21. break;
  22. case 7:
  23. console.log('Sun');
  24. break;
  25. default:
  26. console.log('I dont know');
  27. }
  28. }
  29. // 写法一
  30. function test(day) {
  31. var weekday = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'];
  32. weekday[day - 1] !== undefined ? console.log(weekday[day - 1]) : console.log('i dont know')
  33. }
  34. test(-1)
  35. //写法二
  36. function test(day) {
  37. var weekday = [, 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'];
  38. weekday[day] !== undefined ? console.log(weekday[day]) : console.log('i dont know')
  39. }
  40. test(2);
  1. //考点:预编译作用域闭包
  2. function Test(a, b, c) {
  3. var d = 0;
  4. this.a = a;
  5. this.b = b;
  6. this.c = c;
  7. function e() {
  8. d++;
  9. console.log(d);
  10. }
  11. this.f = e;
  12. }
  13. var test1 = new Test();
  14. test1.f(); //1
  15. test1.f(); //2
  16. var test2 = new Test();
  17. test2.f(); //1
  1. //考点:类数组也是对象的一种形式
  2. function test() {
  3. console.log(typeof (arguments)); //object
  4. }
  5. test();
  1. //考点:函数表达式忽略函数名
  2. var test = function a() {
  3. return 'a';
  4. }
  5. test();
  6. console.log(typeof (a)); //undefined
  1. //考点:typeof(未声明变量) -> undefined
  2. var test = function a() {}
  3. test();
  4. console.log(typeof (a)); //'undefined'