<script>
//创建构造函数
var CreateObj = function(){
//创建属性
this.domain = 'www.php.cn';
//创建方法
this.get = function(value){
var site = 'php中文网';
return site + value;
}
};
var obj = new CreateObj();
var obj1 = new CreateObj();
console.log(obj.domain);
console.log(obj1.get(obj1.domain));
//实例共享属性和方法 prototype
CreateObj.prototype.email = 'admin@admin';
CreateObj.prototype.hello = function(){
return 'JS很好玩';
}
console.log(obj1.email);
console.log(obj1.hello());
</script>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号