js函数

作者: 常连海 | 来源:发表于2017-06-12 23:51 被阅读0次

1.------------------------>>验证

//1和2区别: 预解释问题,函数前后执行问题
//验证名称
function checkName(name) {

}

//验证邮箱
function checkEmail(email) {

}

//验证密码
function checkPassword(password) {

}

2.------------------------>>

var checkName = function () {
    return 'checkName';
};
var checkEmail = function () {
    return 'checkEmail';
};
var checkPassword = function () {
    return 'checkPassword';
};

3.------------------------>>对象封装

var checkObj = {
    checkName: function () {

    },
    checkEmail: function () {

    },
    checkPassword: function () {

    }
};
checkObj.checkName();

/4.------------------------->>函数封装(函数也是对象,函数三角色,对象,类,函数)

//对象不能复制一份,用new新创建的对象,不能继承这些方法
// 将函数当作对象来使用的
var checkObj = function () {
};
checkObj.checkName = function () {

};

checkObj.checkEmail = function () {

};
checkObj.checkPassword = function () {

};
var s = new checkObj;
console.dir(s); //没有这三个方法

5.-------------------------->> 克隆他们的方法,而不是直接调用,和checkObj没有关系

var checkObj = function () {
    return {
        checkName: function () {

        },
        checkEmail: function () {

        },
        checkPassword: function () {

        }
    }
};

var a = checkObj();
a.checkName();

6------------------------->> JS类封装 (类名第一个字母大写,)

var CheckObj = function () {  //公用方法每个对象克隆一份,浪费资源
    this.checkName = function (name) {

    },
        this.checkEmail = function (email) {

        },
        this.checkPassword = function (password) {

        }
};

var a = new CheckObj();   // new 一个类(函数)执行了四步操作: 1.函数执行 2.以当前类实例运行函数,3给这个实例对象绑定属性和方法,this.xx =xx可以绑定上,4.返回这个实例对象
a.checkName();

7.-------------------------->>JS原型(prototype)

var CheckObj = function () {

};
CheckObj.prototype.checkName = function () {
};
CheckObj.prototype.checkEmail = function () {
};
CheckObj.prototype.checkPassword = function () {
};

//简化
var CheckObj = function () {
};
CheckObj.prototype = {
    constructor: CheckObj, //constructor指向类本身
    checkName: function () {
        return this;
    },
    checkEmail: function () {
        return this;
    },
    checkPassword: function () {
        return this;
    }
};

var a = new CheckObj();
a.checkName().checkEmail().checkPassword();
//链式写法 return this
var checkobj = {
    checkName: function (name) {
        return this;
    },
    checkEmail: function (email) {
        return this;
    },
    checkPassword: function (password) {
        return this;  //最终返回这个对象
    }
};

checkobj.checkName('clh').checkEmail(960526415).checkPassword(123456);  //链式写法 ,又返回这个对象


//函数祖先
Function.prototype.checkName = function (name) {   //这样内置内添加方法,会有覆盖问题的
    console.log(this === f); //--->>true 就是小f
    if (name === 'clh')
        console.log(name);
};
var f = function () {
};
f.checkName('clh'); //函数执行,前面有点,this就是点前面


//动态给Function类添加方法, 这样是加的私有属性
Function.prototype.addMethod = function (name, fn) {
    this[name] = fn;
    return this;
}
var methods = function () {
};
methods.addMethod('checkName', function () {
    return this;
}).addMethod('checkEmail', function () {
    return this;
}).addMethod('checkPassword', function () {
    return this;
});
//在methods函数内增加三个私有的属性
console.log(methods.hasOwnProperty('checkName')); //----->>true 是私有属性
methods.checkName().checkEmail().checkPassword();

//链式添加方法
// 创建一个类,把你要添加的方法放到里面,
Function.prototype.myAddMenthod = function (name, fn) {
    this.prototype[name] = fn;
    return this;
}

var CheckObject = function () {
};
CheckObject.myAddMenthod('checkName', function () {
    console.log('checkName');
    return this;
}).myAddMenthod('checkEmail', function () {
    return this;
}).myAddMenthod('checkPassword', function () {
    return this;
});

var methods = new CheckObject();
methods.checkName(name).checkEmail().checkPassword()


Array.prototype.myAddMenthod = function (name, fn) {
    this[name] = fn;
    return this;
};
var ary = [1, 3, 4, 6, 0, 0, 0, 0, 1, 2, 3, 4, 4];
ary.myAddMenthod('unique', function () {
    var obj = {};
    for (var i = 0; i < this.length; i++) {
        var curValue = this[i];
        if (obj[curValue] === curValue) {
            this[i] = this[this.length - 1];
            this.length--;
            i--;
            continue;
        }
        obj[curValue] = curValue
    }
    return this;

}).myAddMenthod('getMaxValue', function () {

    return Math.max.call(undefined, this);
}).myAddMenthod('getMinValue', function () {
    return Math.min.call(undefined, this);
});

console.log(ary.unique().max().min());

相关文章

网友评论

      本文标题:js函数

      本文链接:https://www.haomeiwen.com/subject/ahbrqxtx.html