1 Star 0 Fork 0

mengmenghao / 我的个人网站

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
shiyan5.html 2.49 KB
一键复制 编辑 原始数据 按行查看 历史
mengmenghao 提交于 2020-04-24 22:37 . 第五次
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>上机实验5</title>
</head>
<body>
<script type="text/javascript">
/* 179000522 廖孟豪 */
// 1.字面量创建对象
var obj1 = {
name: "刘备",
sex: "",
run: function() {
console.log("");
}
}
console.log(obj1);
// 2.object方式创建对象
var obj2 = new Object();
obj2.name = "刘备";
obj2.sex = "";
obj2.run = function() {
console.log("");
}
console.log(obj2);
// 3.工厂方式创建对象
function createObject(o_name, o_sex, o_run) {
var tempObject = new Object();
tempObject.name = o_name;
tempObject.sex = o_sex;
tempObject.run = o_run;
return tempObject;
}
var obj3 = createObject("刘备", "", new Function("console.log('跑')"));
console.log(obj3);
// 4.使用构造函数创建对象
function Student(name, sex) {
this.name = name;
this.sex = sex;
this.run = function() {
console.log(this.name + "");
}
}
var obj4 = new Student("刘备", "");
console.log(obj4);
// 5.使用原型模式创建对象
function Person() {}
Person.prototype.name = "刘备";
Person.prototype.sex = "";
Person.prototype.run = function() {
console.log(this.name + "");
}
var obj5 = new Person();
console.log(obj5);
obj5.run();
// 6.构造函数(创建属性)+原型模式(创建方法)创建对象
function TrueGod(name, sex) {
this.name = name;
this.sex = sex;
}
TrueGod.prototype.run = function() {
console.log(this.name + "");
}
var obj6 = new TrueGod("刘备", "");
console.log(obj6);
obj6.run();
// 7.属性对象的特征名称及对应值
function Gods(name) {
this.name = name;
}
var obj7 = new Gods("刘备");
console.log(Object.getOwnPropertyDescriptor(obj7, "name"));
/*
* value: "刘备"
* writable: true【writable】 该属性是否可写
* enumerable: true【enumerable】 是否能在for-in循环中遍历出来或在Object.keys中列举出来
* configurable: true【configurable]】如果为false,
* 则任何尝试删除目标属性或修改属性以下特性(writable, configurable, enumerable)的行为将被无效化,
* */
// 8.this指向
function fun() {
console.log(this)
}
fun();
/* 函数在全局执行环境中,所以其中的this指向的是全局对象,即在浏览器中指向的就是window对象 */
</script>
</body>
</html>
1
https://gitee.com/mengmeng111/website.git
git@gitee.com:mengmeng111/website.git
mengmeng111
website
我的个人网站
master

搜索帮助