1. 对象的构成

  • 属性 + 方法

    1. var teacher = {
    2. name: "Tom",
    3. age: 32,
    4. sex: "male",
    5. weight: 130,
    6. teach: function() {
    7. console.log("I am teaching JavaScript")
    8. }
    9. }

    2. 对象的操作

  • delete teacher.sex; delete teacher.teach;

    3. this

    例1:
    1. var teacher = {
    2. name: "Tom",
    3. age: 32,
    4. sex: "male",
    5. weight: 130,
    6. teach: function() {
    7. console.log("I am teaching JavaScript")
    8. },
    9. smoke: function() {
    10. // teacher.weight --
    11. this.weight --;
    12. // console.log(teacher.weight);
    13. console.log(this.weight);
    14. },
    15. eat: function() {
    16. // teacher.weight ++
    17. this.weight ++;
    18. // console.log(teacher.weight);
    19. console.log(this.weight);
    20. }
    21. }
    例2:课堂考勤管理
    1. var attendance = {
    2. students: [],
    3. total: 6,
    4. join: function(name) {
    5. this.students.push(name);
    6. if (this.students.length === this.total) {
    7. console.log(name + "到课,学生已到齐");
    8. } else{
    9. console.log(name + "到课,学生未到齐");
    10. }
    11. },
    12. leave: function(name) {
    13. var idx = this.students.indexOf(name)
    14. if( idx !== -1) {
    15. this.students.splice(idx, 1)
    16. }
    17. console.log(name + "早退")
    18. console.log(this.students);
    19. },
    20. classOver: function () {
    21. this.students = [];
    22. console.log("已下课")
    23. }
    24. }

    4. 构造函数创建对象

    4.1 系统自带的构造函数

    与对象字面量没有区别 ```javascript // 对象字面量 var teacher = { name: “Tom”, age: 32, } obj.age = 28;

// 自带的Object构造函数 var obj = new Object(); obj.name = “Tom”; obj.age = 23;

  1. <a name="x5vBc"></a>
  2. #### 4.2 自定义构造函数
  3. - 大驼峰
  4. ```javascript
  5. function Teacher() {
  6. this.name = "Tom";
  7. this.sex = "male";
  8. this.weight = 100;
  9. this.eat = function () {
  10. this.weight += 2;
  11. console.log(this.weight)
  12. }
  13. }
  14. var teacher = new Teacher();
  15. // 构造函数传参
  16. function Teacher(opt) {
  17. this.name = opt.name;
  18. this.sex = opt.sex;
  19. this.weight = opt.weight;
  20. this.eat = function () {
  21. this.weight += 2;
  22. console.log(this.weight)
  23. }
  24. }
  25. var teacher = new Teacher({name: "张三", sex: "male", weight: 112})
  • 构造函数生成的对象是独立的,对象A属性的改变不会影响对象B
  • 正式开发时构造函数需使用opt对象来传参

    5. 构造函数与实例化原理

    ```javascript function Car(color, brand) { // this = {}; this.color = color; this.brand = brand; // return this; }

var car1 = new Car(‘red’, ‘Benz’);

GO = { Car: (function), car1: Car{color: ‘red’, brand: ‘Benz’} }

AO = { this: {}, // 当使用new关键字执行时,自动在AO中创建一个this对象 }

  1. **this指向问题**
  2. - 未执行时, this无意义
  3. - 作为普通函数执行时`Car()`thiswindow
  4. - 作为构造函数执行时`new Car()`,会在AO中创建一个新的对象,并将this指向这个对象,函数执行结束后返回这个对象,即**改变this指向**
  5. **构造函数返回值**
  6. - 未指定返回值时,自动返回实例化对象
  7. - 指定返回值时
  8. - 当返回原始值,无效,还是返回实例化对象
  9. - 当返回引用值时(对象,数组,函数),返回该引用值
  10. <a name="hQC2z"></a>
  11. ### 6 包装类
  12. ```javascript
  13. // 原始值没有自己的属性和方法
  14. var a = 1; // a 是原始值
  15. a.len = 3; // new Number(a).len = 3; delete
  16. a.len // undefined
  17. // 数字不一定都是原始值
  18. var b = new Number(a); // b 是对象,可以设置 属性和方法
  19. b.len = 3;
  20. b // Number(1, len: 3)
  21. // Number 对象可以参与运算,返回原始值
  22. var c = b + 1;
  23. c // 2
  24. // String
  25. var str = 'abc';
  26. console.log(str.length) // console.log(new String(str).length) -> 3
  27. str.length = 1; // new String(str).length = 1; delete;
  28. console.log(str.length) // console.log(new String(str).length) -> 3

系统内置的三个包装类
Number(), String(), Boolean()

7. 练习

  1. 写一个构造函数,接收数字类型的参数,参数数量不定,完成参数相加和相乘的功能。 ```javascript // 方法1 function Compute() { var args = arguments, res; this.getSum = function() { res = 0; for(var i = 0; i < args.length; i++) { res += args[i] } console.log(res) };

    this.getProduct = function() { res = 1; for(var i = 0; i < args.length; i++) { res *= args[i] } console.log(res) } }

// 方法2 function Compute() { var args = arguments;

function loop(method, res) { for(var i = 0; i < args.length; i++) { var item = args[i]; if(method === ‘add’) { res += item } else if (method === “mul”) {res *= item} } console.log(res) }

this.getSum = function() { loop(“add”, 0); };

this.getProduct = function() { loop(“mul”, 1); } }

var compute = new Compute(1, 2, 3, 4, 5); compute.getSum(); compute.getProduct();

// 方法3 function Compute() {
function loop(args, method, res) { for(var i = 0; i < args.length; i++) { var item = args[i]; if(method === ‘add’) { res += item } else if (method === “mul”) {res *= item} } console.log(res) }

this.getSum = function() { loop(arguments, “add”, 0); };

this.getProduct = function() { loop(arguments, “mul”, 1); } } var compute = new Compute(); compute.getSum(2, 4, 6); compute.getProduct(3, 5, 7);

  1. 2. 消费者与车
  2. 写一个构造车的函数,可设置车的品牌,颜色,排量;再写一个构造消费者的函数,设置用户的名字,年龄,收入,通过选择的方法实例化该用户喜欢的车,再设置车的属性。
  3. ```javascript
  4. function Car(opt) {
  5. this.brand = opt.brand;
  6. this.color = opt.color;
  7. this.capacity = opt.capacity;
  8. }
  9. function Consumer(opt) {
  10. this.name = opt.name;
  11. this.age = opt.age;
  12. this.income = opt.income;
  13. this.selectCar = function() {
  14. this.favorCar = new Car(opt.carOpt);
  15. console.log(this.favorCar)
  16. }
  17. }
  18. var consumer = new Consumer({
  19. name: "Alex",
  20. age: 32,
  21. income: 100000,
  22. carOpt: {
  23. brand: 'Tesla',
  24. color: "silver",
  25. capacity: 20
  26. }
  27. });
  28. consumer.selectCar()
  1. 构造函数与闭包 ```javascript function Test(a, b) { var c = 1; this.a = a; this.b = b;

    function f() { c++; console.log(c); }

    this.g = f; }

var test1 = new Test(); test1.g(); // 2 test1.g(); // 3 var test2 = new Test(); test2.g(); // 2

  1. 4. 预编译
  2. ```javascript
  3. var x = 1, y = z = 0;
  4. function add(n) {
  5. return n = n + 1;
  6. }
  7. y = add(x);
  8. function add(n) {
  9. return n = n + 3;
  10. }
  11. z = add(x);
  12. console.log(x, y, z) // 1, 4, 4
  1. 立即执行函数 ```javascript function foo1(x) { console.log(arguments); return x; }

foo(1, 2, 3, 4, 5); // [1,2,3,4,5]

function foo2(x){ console.log(arguments); return x; }(1,2,3,4,5); // 5

(function foo3(x){ console.log(arguments); return x; })(1,2,3,4,5); // [1,2,3,4,5]

  1. 6. 形参实参映射
  2. ```javascript
  3. function b(x, y, a) {
  4. a = 10;
  5. console.log(arguments[2]); // 10
  6. arguments[2] = 10;
  7. console.log(a); // 10
  8. }
  9. b(1, 2, 3)

ASCII码:表1: 0-127, 表2: 128-255 一个字节 byte
UNICODE码,涵盖ASCII码, 2个字节

  1. var str = 'a';
  2. // string.charCodeAt() -> 返回字符串某位置上字符的unicode码
  3. var pos = str.charCodeAt(0);
  4. pos // 97

写一个函数,接收任意一个字符串,算出这个字符串的总字节数

  1. function getBytes(str) {
  2. var bytes = 0;
  3. for(var i = 0; i < str.length; i++) {
  4. var charCode = str.charCodeAt(i);
  5. if (charCode <= 255) {
  6. bytes += 1;
  7. } else {
  8. bytes += 2;
  9. }
  10. }
  11. return bytes;
  12. }
  13. getBytes("Tom, 你好,我是Alex"); // 19

8. 其他

  1. 数组的截断方法
    1. var arr = [1, 2, 3, 4, 5]
    2. arr.length = 3;
    3. console.log(arr) // [1, 2, 3]