JS之创建自己的对象

来源:互联网 发布:印度英语的特点知乎 编辑:程序博客网 时间:2024/05/18 10:11
JS之创建自己的对象 - lishirong - The CTO of LiShirong
 
<html>
<head>
<title>创建自己的对象</title>
<script language="javascript">
function past(grain, width, shape, hasEgg)
{
    this.grain = grain;
    this.width = width;
    this.shape = shape;
    this.hasEgg = hasEgg;  
    this.toString = pastToString;
}
function pastToString()
{
    // 返回对象的属性。

    return "Grain: " + this.grain + "\n" +
        "Width: " + this.width + "\n" +
        "Shape: " + this.shape + "\n" +
        "Egg?: " + Boolean(this.hasEgg);
}
var spaghetti = new past("wheat", 0.2, "circle", true);
window.alert(spaghetti);
</script>
</head>
<body>

</body>
</html>
0 0
原创粉丝点击