javascript - 在Javascript中通过函数获取所有继承的属性

标签 javascript properties

不使用函数,当我使用 for (var prop in obj) 时,它将显示对象中的所有值。然而,当我尝试放入函数时,它只向我显示对象中的第一个值,而不向我显示第二个元素(studentID)。 :(

var target = document.getElementById("outputArea");
var outstring = " ";

var myObj = {
	name: "Nguyen Viet Tien",
	StudentID: "26813157",
};

function tellAll(obj) {
	var dis = " ";
	
	for(var prop in obj) {
		dis += "first property is" + prop + "with the content" + obj[prop] + "<br/>";	
		return dis;
	}
}

outstring += tellAll(myObj);
target.innerHTML = outstring;
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Eng1003 Workshop Code Week 04</title>
	<style>
		#outputArea {
			padding: .25em;
			border: solid black 2px;
			margin: 3em;			
			height: 20em;
			width: 20em;
			overflow-y: scroll;
			font-family: arial "sans serif";
			font-size: 1em;
			color: rgb(50, 50, 250);
			background-color: rgb(225,225,225) ;
		}
	</style>
</head>

<body>
	<div id="outputArea"></div>
</body>
</html>

最佳答案

返回语句在错误的位置

function tellAll(obj) {
    var dis = " ";

    for(var prop in obj) {
        dis += "first property is" + prop + "with the content" + obj[prop] + "<br/>";   
    }

    return dis;
}

关于javascript - 在Javascript中通过函数获取所有继承的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32053674/

相关文章:

javascript - 单击元素以更改另一个元素的背景图像

javascript - 检查日期和时间是否需要检查时区?

python-2.7 - 在kivy python中在屏幕之间交换变量

Objective-C 为属性设置默认值

javascript - 如何使用 Jquery 对货币进行四舍五入

javascript - 等待新打开的窗口关闭后再继续

.net - 为什么too​​lstriplabel 的backcolor 属性在设计时或运行时不会改变?

Objective-C 字典作为属性/方法访问

java - 在属性文件中使用属性

javascript - 更新 apollo 客户端实例的 uri