JavaScript之原型、原型链、call/apply

原型

共有属性提取出来放到 prototype 中:

// Person.prototype – 原型
// Person.prototype = {} – 祖先

	Car.prototype = {
      height: 1400,
      lang: 5000,
      name: 'bm',
    }

    function Car() {

    }
    var car = new Car();

例1.

Person.prototype.name = 'sunny';

    function Person() {

    }
    var person = new Person();
    Person.prototype.name = 'cherry';

    // person.name  =>  cherry

例2.

Person.prototype.name = 'sunny';

    function Person() {

    }
    var person = new Person();
    // Person.prototype.name = 'cherry';

    Person.prototype = {
      name: 'cherry'
    }

    // person.name  =>  sunny

例3.

Person.prototype.name = 'sunny';

    function Person() {

    }
    Person.prototype = {
      name: 'cherry'
    }
    var person = new Person();

    // person.name  =>  cherry

原型链

Grand.prototype.lastName = "Deng";

    function Grand() {

    }
    var grand = new Grand();
    Father.prototype = grand;

    function Father() {
      this.name = 'ming'
    }
    var father = new Father();
    Son.prototype = father;

    function Son() {
      this.name = 'smoke'
    }
    var son = new Son();

    // son.lastName  // Deng
	Person.prototype = {
      name: 'a',
      sayName: function () {
        console.log(this.name);
      }
    }

    function Person() {
    }
    var person = new Person();

    // person.sayName    // f (){console.log(this.name);}
    // person.sayName()  // a


	Person.prototype = {
      name: 'a',
      sayName: function () {
        console.log(this.name);
      }
    }

    function Person() {
    	this.name = "b";
    }
    var person = new Person();
	// person.sayName()  // b
//相同
var obj = {};  ==  var obj = new Object();
  • Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数;
  • Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数;
  • Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数(这也是我们在数学课上学到的舍入规则)。

取1~100之间的随机数

for (var i = 0; i < 10; i++) {
      var num = Math.floor(Math.random() * 100);
      console.log(num);
    }

 
**call/apply **
call和apply方法区别:改变this指向,传参列表不同;

call 需要把实参按照形参的个数传进去;
apply 需要传一个arguments(数组);

例1:

// call 方法 改变了this的指向;
function Person(name, age) {
      this.name = name;
      this.age = age;
    }
    var person = new Person('deng', 20);
    var obj = {

    }
    Person.call(obj, 'chen', 30);

    //obj
    // {
    //   age: 30
    //   name: "chen"
    // }

 
例2:

function Person(name, age, sex) {
      this.name = name;
      this.age = age;
      this.sex = sex;
    }

    function Student(name, age, sex, tel, grade) {
      Person.call(this, name, age, sex);
      this.tel = tel;
      this.grade = grade;
    }
    var student = new Student('sunny', 123, 'male', 139, 2017);

 
apply

function Person(name, age, sex) {
      this.name = name;
      this.age = age;
      this.sex = sex;
    }

    function Student(name, age, sex, tel, grade) {
      //Person.call(this, name, age, sex);
      Person.apply(this, [name, age, sex]);
      this.tel = tel;
      this.grade = grade;
    }
    var student = new Student('sunny', 123, 'male', 139, 2017);

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值