对象函数原型中名称的区别是什么?
What is the difference in names in an object function prototype?
如果我在JavaScript中创建一个对象,然后用prototype
关键字添加一个函数,为什么这两个名称不能不同?
我有一个函数叫getName
,另一个叫whatName
。我不能调用whatName
而没有错误。命名函数和匿名函数有什么区别?
哪一种方法更受欢迎?
代码:Person.prototype.getName = function whatName() {
return this.name;
};
代码:<script>
var Person = function Person(n,f,m,l) {
this.name = n;
this.lname = l;
this.fname = f;
this.mname = m;
this.callMeth1 = function jjj() {
}
this.callMeth2 = function () {
}
this.callMeth3 = function () {
}
};
Person.prototype.getName = function () {
return this.name;
};
var test = new Person("Doug");
Person.prototype.sayMyName = function() {
alert('Hello, my name is ' + this.getName());
};
test.sayMyName();
</script>
代码:function callMyMeth4 (a,b) {
var aaa = a;
var bbb = b;
}
var Person = function Person(n,f,m,l) {
this.name = n;
this.lname = l;
this.fname = f;
this.mname = m;
this.callMeth1 = function () {
}
this.callMeth2 = function () {
}
this.callMeth3 = function () {
}
this.callMeth3 = callMyMeth4(a,b);
};
你可以这样做:
Person.prototype.getName = function () {
return this.name;
};
如果你想使用一个已经存在的函数,那么你可以这样做:
Person.prototype.getName = whatName;
whatName是前面声明的函数的名称
相关文章:
- 这是什么 ==- javascript 运算符
- 我的单元测试选项是什么
- 打破承诺链的好方法是什么
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- Javascript中的空白是什么
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- 在JavaScript中拆分日期字符串的更好方法是什么
- 将jQuery.ech()方法转换为本地JavaScript抽象的最佳方法是什么
- 处理浮点错误的最佳方法是什么
- javascript导入的最佳实践是什么
- 基于窗口宽度jquery的函数的替代方法是什么
- 在ng重复循环中显示条件内容的最佳方式是什么
- 我的客户端选项是什么
- 这是什么“;要求“;事情
- 未捕获的类型错误:topFrame.window.changeSelectedBarStyle不是函数,原因是什么
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- @@(“at at”)在ES6 JavaScript中是什么意思
- knex:根据结果创建数组的合适方法是什么
- emu -model、emu -restless和emu之间的主要区别(优缺点)是什么?
- function()在jQuery中是什么意思?这个和$(这个)有什么区别