/*对象,包装类(上)*/
// var obj = { //对象是一种基础的变量类型,它和数组和function都属于引用值
// } //对象有属性和方法
// delete = 删除代码
//当一个变量没经过声明就使用的话会报错,当一个对象的属性没有就使用的话,就会打印undefined
/*对象的创建方法*/
// 1.var obj = {} plainObject --> 对象字面量/对象直接量
// 2.构造函数 [28:33 --> ]
// 1) 系统自带的构造函数 new Object()
// 2) 自定义
/*
构造函数写对象时要用等号
构造函数命名规则:大驼峰式命名规则
*/
// 构造函数 例:[41:17 --> 46:28]
// function Car () {
// this.name = '玛莎拉蒂';
// this.height = '1400';
// this.lang = '200';
// this.weight = 1000;
// this.health = 100;
// this.run = function () {
// this.health --;
// }
// }
// var car = new Car();
// var car1 = new Car();
// car.name = '兰博基尼';
// car1.name = '法拉利';
//例2:[57:26 --> 结束]
// function Student(name, age, sex) {
// this.name = name;
// this.age = age;
// this.sex = sex;
// }
// var student = new Student("li", 18, "nan");
/*对象包装类下*/
// 构造函数必须要加new [05:33 --> 07:23]
/*
构造函数内部原理
1.在函数体最前面隐式的加上 this = {}
2.执行this.xxx = xxx;
3.隐式的返回this
*/
//例1 [07:29 --> 09:50]
// function Person(name, height, say) {
// //var this = {}
// this.name = name;
// this.height = height;
// this.say = function () {
// console.log(this.say)
// }
// // return this;
// }
// console.log(new Person('xiaowang').name);
//例2[10:09 -->13:50]
// function Person(name, height) {
// var thar = {};
// thar.name = name;
// thar.height = height;
// return thar;
// }
// var person = Person('xiaowang', 180);
// var person1 = Person('xiaozhang', 175);
/*包装类*/
// new String();
// new Boolean();
// new Number();
/*
[14:23 --> 20:03]
原始值是不可以有属性和方法的,属性和方法只有对象可以有,这是对象独特的东西
原始值是不可以有属性和方法的,但是为什么能调用了,因为它经历了一个过程叫做包装类
*/
//例1:[21:48 --> 24:03]
// var num = 4;
// num.len = 3;
// 转换为数字对象 <-- new Number(4).len = 3; --> 赋值给len = 3, 最后delete
// new Number(4).len --> console.log(num.len);
//例2[24:11 --> 29:21]
// var str = "abcd"
// str.length = 2;
// new String('abcd').length = 2; delete
// console.log(str);
// console.log(str.length);
// new String('abcd').length
//例3:[30:00 --> 36:45]
// var str = "abc";
// str += 1;
// var test = typeof(str);
// if (test.leng == 6) {
// test.sign = "typeof的返回结果可能为String"
// new.String(test).sign = "typeof的返回结果可能为String"
// }
// new String(test).sign
// // console.log(test.sign);
/*练习题*/
//例题1:分析下面的javascript代码段:[43:16 --> 45:27]
// function employee(name, code) {
// this.name = "wangli";
// this.code = "A001";
// }
// var newemp = new employee("zhangming","A002")
// document.write("雇员姓名:" + newemp.name + "<br>")
// document.write("雇员姓名:" + newemp.name + "<br>")
// 输出的结果().(选择一项)
// A.雇员姓名: wangli 雇员代码:A001;
// B.雇员姓名:zhangming 雇员代码:A002;
// c.雇员姓名:null 雇员代码:null;
// d.代码有错误,无输入结果
//例题2:[45:38 --> 47:51]
// function Person(name, age, sex) {
// var a = 0;
// this.name = name;
// this.age = age;
// this.sex = sex;
// function sss() {
// a ++;
// document.write(a);
// }
// this.say = sss;
// }
// var oPerson = new Person();
// oPerson.say();
// oPerson.say();
// var oPerson1 = new Person();
// oPerson1.say()
//例题3:下面这段JS代码执行完毕后x,y,z的值分别是多少?[49:44 --> 53:06]
// var x = 1,
// y = z = 0;
// function add(n) {
// return n = n + 1;
// }
// y = add(x);
// function add(n) {
// return n = n + 3;
// }
// z = add(x);
//例题4:下面代码中console.log()的结果是{1, 2, 3, 4, 5}的选项是[56:00 --> 58:00]
//A
// function foo(x) {
// console.log(arguments);
// return x;
// }
// foo(1, 2, 3, 4, 5);
// //B
// function foo(x) {
// console.log(arguments);
// return x;
// }(1, 2, 3, 4, 5);
// //C
// (function foo(x) {
// console.log(arguments);
// return x;
// })(1, 2, 3, 4, 5)
//D
//例题5:请问以下表达式的结果是什么?[59:20 --> 1:01:22]
// parselnt(3,8)
// parselnt(3,2)
// parselnt(3,0)
//例题6:以下哪些是JavaScript语言typeof可能返回的结果 [1:01:22 -->1:01:42]
/*
1.string
2.array
3.object
4.null
*/
//例题7:看看下面alert的结果是什么 [1:01:56 --> 1:04:49]
// function b(x, y, a) {
// a = 10;
// alert(arguments[2]);
// }
// b(1, 2, 3);
// 如果函数体改成下面结果又会如何?
// arguments[2] = 10;
// alert(a);
</script>
对象,包装类
最新推荐文章于 2020-09-02 08:49:04 发布