1.封装typeof

  1. function myTypeof(val){
  2. var type = typeof(val);
  3. var toStr = Object.prototype.toString;
  4. var res = {
  5. '[object Array]': 'array',
  6. '[object Object]': 'object',
  7. '[object Number]': 'object number',
  8. '[object String]': 'object string',
  9. '[object Boolean]': 'object boolean'
  10. }
  11. if(val === null){
  12. return null; //先判断是否是null类型因为 null 是特殊的object型 如果是的话就先排除
  13. }else if(type === 'object'){ //判断是否是引用类型的数据
  14. var ret = toStr.call(val); //判断具体的引用类型
  15. return res[ret]; //返回对象中具体的引用类型
  16. }else{
  17. return type; //若不是引用类型的数据 则直接返回type类型数据
  18. }
  19. }
  20. console.log(myTypeof(new Number(1)));

2.数组去重

数组去重的12种方法

  1. var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN, 'NaN', 0, 0, 'a', 'a', {}, {}];
  2. Array.prototype.unique = function(){
  3. var temp = {},
  4. newArr = [];
  5. for(var i = 0;i<this.length;i++){
  6. if(!temp.hasOwnProperty(this[i])){
  7. temp[this[i]] = this[i];
  8. newArr.push(this[i]);
  9. }
  10. }
  11. return newArr;
  12. }
  13. console.log(arr.unique())

解题思路: 利用键名唯一的特性**hasOwnProperty
设置一个空对象,遍历数组 判断传入的每个数组元素是否为其属性 如果不是则空对象传入此属性
新数组加入此元素.**

字符串去重

  1. var str = '1122333aaaabbbbb';
  2. String.prototype.unique = function(){
  3. var temp = {},
  4. newArr = '';
  5. for(var i=0;i<this.length;i++){
  6. if(!temp.hasOwnProperty(this[i])){
  7. temp[this[i]] = this[i];
  8. newArr+=this[i];
  9. }
  10. }
  11. return newArr;
  12. }
  13. console.log(str.unique());

解题思路: 利用键名唯一的特性**hasOwnProperty
设置一个空对象,遍历字符串 判断每个字符,是否为对象的属性值,若不是则放进新字符串**

打印字符串中只出现过一次的字符组成新的字符串

  1. var str = 'truaiwritwtruibowertpwrisweqx';
  2. function test(str) {
  3. var temp = {},
  4. newStr = '';
  5. for (var i = 0; i < str.length; i++) {
  6. if (temp.hasOwnProperty(str[i])) {
  7. temp[str[i]]++;
  8. } else {
  9. temp[str[i]] = 1;
  10. }
  11. }
  12. for (var key in temp) {
  13. if (temp[key] === 1) {
  14. newStr += key;
  15. }
  16. }
  17. return newStr;
  18. }
  19. console.log(test(str));

解题思路: 利用键名唯一的特性**hasOwnProperty
1.设置一个空对象,遍历字符串 判断每个字符,是否为对象的属性值,若是则将属性的键值++,若不是将属性的键值设置为1
2.遍历所有键值为1的属性 将其放入新数组**

打印字符串中第一次出现且只出现过一次的字符

  1. var str = 'truaiwritwtruibowertpwrisweqx';
  2. function test(str) {
  3. var temp = {},
  4. newStr = '';
  5. for (var i = 0; i < str.length; i++) {
  6. if (temp.hasOwnProperty(str[i])) {
  7. temp[str[i]]++;
  8. } else {
  9. temp[str[i]] = 1;
  10. }
  11. }
  12. for (var key in temp) { //遍历对象
  13. if (temp[key] === 1) {
  14. return key;
  15. }
  16. }
  17. }
  18. console.log(test(str));

解题思路: 利用键名唯一的特性**hasOwnProperty
1.设置一个空对象,遍历字符串 判断每个字符,是否为对象的属性值,若是则将属性的键值++,若不是将属性的键值设置为1
2.遍历所有键值为1的属性 遍历到的第一个输出出来**

习题

1

    function Test(a,b,c){
        var d = 0;
        this.a = a;
        this.b = b;
        this.c = c;
        function e(){
            d++;
            console.log(d);
        }
        this.f = e;
    }
    // var this = {
    //     f:function(){

    //     }
    // }
    // AO:{
    //     d: 0
    // }
    // this.f = function(){
    //     d++;
    //     console.log(d);
    // }
    // return this;
    var test1 =new Test();
    test1.f(); //1
    test1.f(); //2
    var test2 = new Test();
    test2.f(); //1

2

    function test(){
        console.log(typeof(arguments)); //object 因为argumemts是类数组
    }
    test();

3

    var test = function a(){ //    函数表达式在外部直接忽略 此处a被忽略
        // a(); //内部可以访问
        return 'a';
    }
    console.log(test.name);
    // a()//外部无法访问 因为被忽略
    console.log(typeof(a)); //undefined string 类型

4 优化改写下列函数

    function test(day){
        switch (day) {
            case 1:
                console.log('Mon');
                break;
            case 2:
                console.log('Tue');
                break;
            case 3:
                console.log('Wed');
                break;
            case 4:
                console.log('Thu');
                break;
            case 5:
                console.log('Fri');
                break;
            case 6:
                console.log('Sat');
                break;
            case 7:
                console.log('Sun');
                break;
            default:
                console.log('I dot know');
        }
    }
    test(3);

//写法一
    function test(day){
        var weekday = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
        // weekday.splice(0,0,'');
        console.log(weekday);
        weekday[day-1] !== undefined ? console.log(weekday[day-1]) : console.log('I don\'t know');
    }
    test(1);

//写法二 不改变数组的情况下取值下标不改 正确的获取值
    function test(day){
        var weekday = [,'Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
        // weekday.splice(0,0,'');
        console.log(weekday);
        weekday[day] !== undefined ? console.log(weekday[day]) : console.log('I don\'t know');
    }
    test(1);

//写法三
    function test(val) {
        var res = {
            '1': 'Mon',
            '2': 'Tue',
            '3': 'Wed',
            '4': 'Thu',
            '5': 'Fri',
            '6': 'Sat',
            '7': 'Sun'
        }
        // console.log(res[val]);
        return res[val] === undefined ? 'i dont konw' : res[val];
    }
    console.log(test(3));