-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJS lab.js
More file actions
126 lines (98 loc) · 2.71 KB
/
JS lab.js
File metadata and controls
126 lines (98 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 题目1
var a = function(){}
a.b = 1
a.prototype.b = 2
a.prototype.c = 3
a.prototype.d = 4
console.log(a.b) // 1
console.log(new a().b) //2
var foo = new a()
foo.c = 5
console.log(foo.c) //5
console.log(foo.d) //4
//请问console.log的输出?
// 题目2
var Foo = function(){
this.a = 1
return {
a:2
}
}
var bar = new Foo()
console.log(bar.a) //2
//请问console.log的输出?
/* 构造函数不需要返回值。
使用new来创建对象时,如果return的是非对象则会忽而略返回值;
如果return的是对象,则返回该对象。
*/
//题目3
var map = Object.create(null); // 没有原型
console.log("toString" in map); // false
var map = Object.create({a:1});
console.log("toString" in map); // true
console.log("a" in map); //true
//请问console.log的输出?
//题目4
function foo(obj){
return Object.prototype.toString.call(obj).slice(8,-1)
}
//请说明函数foo的作用
/*
用call调用Object.prototype.toString应用在obj上
如果toString()在自定义对象中未被覆盖,则返回 "[object type]"
例如:
Object.prototype.toString.call([]) // "[object Array]"
Object.prototype.toString.call({}) // "[object Object]"
Object.prototype.toString.call(2) // "[object Number]"
再通过slice从正数第8个元素(前面均为“object ”)到倒数第1个(最后)截取字符串
即返回对象的类型
例如:
console.log(foo([]) == 'Array'); //true
*/
//题目5
var a = {}
a.bar = 2
Object.defineProperty(a, "foo",
{ value: "hi"});// 在a对象上定义foo属性
console.log(delete a.foo) //false
console.log(delete a.bar) //true
a.foo = "world"
console.log(a.foo) //hi
for (var key in a){
console.log(key); // 没有输出
}
console.log("foo" in a); //true
console.log("bar" in a); //false
//请问console.log的输出?
/*
当一个属性被设置为不可设置,delete操作将不会有任何效果,并且会返回false。
而Object.defineProperty(obj, prop, descriptor)默认情况下添加的属性值是不可变 且不可枚举
所以foo没有被删除,bar被删除 且for in遍历无输出
*/
//task1
//(1)
function Person(name, age){
this.name = name;
this.age = age;
this.introduce = function(){
return console.log("I am " + name +", I am " + age + " years old!")
}
}
var jerry = new Person("Jerry", 2);
jerry.introduce();
//(2)
function Vector(x,y){
this.x = x;
this.y = y;
this.plus = function(otherV){
return new Vector(this.x + otherV.x, this.y + otherV.y);
}
this.minus = function(otherV){
return new Vector(this.x - otherV.x, this.y - otherV.y);
}
}
//test
var a = new Vector(3,4);
var b = new Vector(5,6);
console.log(a.plus(b));
console.log(a.minus(b));