如何获得构造函数对象的显示属性 [英] How to get display properties of constructor object
问题描述
任务似乎很容易,但是我想我在这里遗漏了一些非常关键的要点. 我有一个看起来像这样的构造函数对象:
The task seems really easy but I think I am missing some very crucial point here.
I have a constructor object which looks like this:
function Person (name) {
this.name = name;
call = function (cellphone, callee){
}
return call;
}
然后我们初始化这个对象
Then we initialize this object
var dan = new Person("Dan");
var mark = new Person("Mark");
var phone = {owner: dan, number: "778-22-345"}
console.log(dan.call(phone, mark))
我想获得如下输出:
I would like to get the output which looks like this
"Dan called Mark from Dan`s phone 778-22-345"
如何访问这些属性?
我尝试过的事情:
我试图建立这样的调用方法
通话=功能(手机,被叫方){
返回名称+被叫" +被呼叫者名称+来自"+手机.所有者+电话" +手机号
}
How can I access those properties?
What I have tried:
I tried to build my call method like this
call = fucntion (cellphone, callee) {
return name + " called " + callee.name + " from " + cellphone.owner + "`s phone " + cellphone.number
}
推荐答案
首先,您的写作在JavaScript中有点危险!不要让JS决定将缺少的行尾(;)放在哪里-这可能会决定您错了...
还应在它属于它的任何时候使用它,并且不要忽略它!
您完全错误地定义了Person
!创建它时,实际上是将call
方法分配给新对象,因为结尾处是return call
!
因此var dan = new Person(''Dan'');
将dan
设置为方法调用,而不是类Person
!!!!!!
First of all your writing is a bit dangerous in JavaScript! Don''t let JS decide where to put thew missing line endings (;) - it will decide probably wrong for you...
Also use this whenever it belongs and do not omit it!
You definedPerson
in a totally wrong way! When you create it you actually assign thecall
method to the new object because of thereturn call
at the end!!!
Sovar dan = new Person(''Dan'');
setsdan
to be the method call and NOT the classPerson
!!!
function Person (name) {
this.name = name;
this.call = function (cellphone, callee) {
// your code here...
};
};
面向对象的JavaScript简介-JavaScript | MDN [ ^ ]
Introduction to Object-Oriented JavaScript - JavaScript | MDN[^]
这篇关于如何获得构造函数对象的显示属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!