如何访问函数/类属性javascript [英] how to access function/class properties javascript

查看:70
本文介绍了如何访问函数/类属性javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我在弄清楚如何记录(在控制台中)PenguinnamenumLegs属性(即"emperor"和2),而没有更改函数的内部?

In the following code, I am having trouble figuring out how to log (in the console) the name and numLegs properties of Penguin (i.e., "emperor" and 2), without changing the inside of the function?

function Penguin(name) {
    this.name = name;
    this.numLegs = 2;
}

var emperor = new Penguin("emperor");

我该怎么办?

推荐答案

只需访问它的属性.皇帝是一个对象,这意味着您可以使用.语法访问属性.

Simply accessing it's properties. Your emperor is an object, which means that you can access properties with . syntax.

function Penguin(name){

   this.name=name;

   this.numLegs=2;

}

var emperor = new Penguin("emperor");

console.log(emperor.name);
console.log(emperor.numLegs);

这篇关于如何访问函数/类属性javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆