20-1 向实例中添加自定义属性和方法.htm

来自「JAVASCRIPT完全自学手册,中源码的验证修订实例」· HTM 代码 · 共 43 行

HTM
43
字号
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=GB2312" />
<title>20-1  向实例中添加自定义属性和方法</title>
<style>
* { font-size:12px; font-family:宋体, Arial; } /*规定了所有的字体样式*/
</style>
<script>
function User(name){
    this.name;
    this.init = function(name){
        if(!name){
            this.name = "默认名称";
        }else{
            this.name = name;
        }
        this.print("我已初始化结束");
    }
    this.login = function(password){
        this.print("我在执行登录操作");
    }
    this.print = function(str){
        document.write("["+this.name+"]:"+str+"<br>");
    }
    this.init(name);
}

//创建新的“User”类实例
user1 = new User("hutia");
user1.newName = "hutia2";
user1.remove = function(){ this.print("我被移除了!"); }
//创建新的“User”类实例
user2 = new User("axiang");
//调用“User类”实例的方法
user1.print("我的新属性\"newName\"的值是\""+user1.newName+"\"");
user2.print("我的新属性\"newName\"的值是\""+user2.newName+"\"");
user1.print("我的新方法\"remove\"的内容是\""+user1.remove+"\"");
user2.print("我的新方法\"remove\"的内容是\""+user2.remove+"\"");
</script>
</head>
<body>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?