有什么区别
var A = function () {
this.x = function () {
//do something
};
};
Run Code Online (Sandbox Code Playgroud)
和
var A = function () { };
A.prototype.x = function () {
//do something
};
Run Code Online (Sandbox Code Playgroud) 我想知道使用这些中的任何一个是否有任何优势,我应该走哪条路?
构造方法:
var Class = function () {
this.calc = function (a, b) {
return a + b;
};
};
Run Code Online (Sandbox Code Playgroud)
原型方法:
var Class = function () {};
Class.prototype.calc = function (a, b) {
return a + b;
};
Run Code Online (Sandbox Code Playgroud)
我不喜欢这样,使用原型,方法定义与类分开,我不知道是否有任何特定的原因我应该使用它而不是第一种方法.
此外,使用函数文字定义"类"是否有任何好处,而不仅仅是函数定义:
var Class = function () {};
Run Code Online (Sandbox Code Playgroud)
VS
function Class () {};
Run Code Online (Sandbox Code Playgroud)
谢谢!
这是代码吗
function Person() {
function myMethod() {
alert ('hello');
}
this.method = myMethod;
}
Run Code Online (Sandbox Code Playgroud)
相当于:
function Person() { }
Person.prototype.method2 = function() {
alert ('hello');
};
Run Code Online (Sandbox Code Playgroud)
如果是,我应该使用哪种方法定义?为什么?
我有以下代码定义了一个Car.每个Car都有一个颜色,还有一个setColor(color)功能.我想添加每次调用时setColor(color)调用的侦听器函数,我希望能够随时随地处理这些侦听器函数.这是一种合适的方法吗?有更干净的方式吗?
function Car() {
this._color = 'red';
this._callbacks = {};
this.setColor = function(color) {
this._color = color;
console.log(">>> set car color to " + color);
if (this._callbacks['setColor']) {
this._callbacks['setColor']();
}
};
this.addListener = function(functionName, handler) {
if (this._callbacks[functionName]) {
var oldCallback = this._callbacks[functionName];
this._callbacks[functionName] = function() {
oldCallback();
handler();
}
} else {
this._callbacks[functionName] = function() {
handler();
}
}
};
}
var car = new Car();
car.setColor('blue');
car.addListener('setColor', function() { console.log("This …Run Code Online (Sandbox Code Playgroud) 以下代码有什么区别:
changeName(): ng.IPromise<any>;
Run Code Online (Sandbox Code Playgroud)
和
changeName: () => ng.IPromise<any>;
Run Code Online (Sandbox Code Playgroud)
我知道一个是返回类型,但我对第一个感到困惑.
这是函数体:
changeName = (): ng.IPromise<any> => {
var self = this;
self.chnAction = "PREFERENCES.CHANGE_NAME.SUBMITTING_BUTTON_TEXT";
self.chnErrorMessage = null;
return self.uss.changeName(
self.chnNewFirstName,
self.chnNewLastName)
.then(
(response: ng.IHttpPromiseCallbackArg<any>): any => {
self.chnAction = "PREFERENCES.CHANGE_NAME.SUBMITTED_BUTTON_TEXT";
self.chnNewFirstName = '';
self.chnNewLastName = '';
self.chnErrorMessage = null;
self.logout();
return this.$state.go('home.auth', { content: 'change_name_success' });
},
(error: ng.IHttpPromiseCallbackArg<any>): ng.IPromise<any> => {
if (error.status === 500) {
self.chnErrorMessage = 'AUTH_SERVICE.UNABLE_TO_CONTACT_SERVER';
} else {
var errors: string[] = [];
Object.keys(error.data.modelState).forEach((key) => { …Run Code Online (Sandbox Code Playgroud) 这是我的代码 -
function searchString(usrLogin) {
var setUsrLogin = function (usrLogin) {
this.usrLogin = (usrLogin == "") ? "*" : usrLogin;
}
this.toString = function(){
return 'source="dbmon-dump://Source/ID" ' + 'USR_LOGIN="' + this.usrLogin + '" ';
}
setUsrLogin(usrLogin);
}
$(function() {
var a = new searchString("");
$('#searchBar').val(a.toString());
});
Run Code Online (Sandbox Code Playgroud)
a.toString()prints source ="dbmon-dump:// Source/ID"USR_LOGIN ="undefined"因为this.usrLogin显示为未定义.我究竟做错了什么?
javascript ×6
prototype ×3
oop ×2
callback ×1
function ×1
jquery ×1
listener ×1
methods ×1
this ×1
typescript ×1