zoukankan      html  css  js  c++  java
  • 【JS】(+﹏+)~

    1

    var o = new Object()
    var o = new Object // 如果没有参数,括号可以省略

    2

    this.init.apply(this, arguments) ???

    // 创建类
    var Class = function() {
        var klass = function() {
            this.init.apply(this, arguments)
        }
        klass.prototype.init = function() {}
        return klass
    }
    
    var Person = new Class()
    Person.prototype.init = function() {
    }
    
    var person = new Person()

    what does this usage of apply() means in Javascript

    ;(function() {
        var Klass = function(arg1, arg2, arg3) {
            this.init(arg1, arg2, arg3)
        }
        Klass.prototype.init = function(arg1, arg2, arg3) {
            console.log(arg1, arg2, arg3)
        }
        var klass = new Klass(1, 2, 3) 
    })()
    
    ;(function() {
        var Klass = function() {
            this.init.apply(this, arguments)
        }
        Klass.prototype.init = function() {
            console.log(arguments[0], arguments[1], arguments[2])
        }
        var klass = new Klass(1, 2, 3) 
    })()
    
    ;(function() {
        var Klass = function() {
            this.init(arguments[0], arguments[1], arguments[2])
        }
        Klass.prototype.init = function() {
            console.log(arguments[0], arguments[1], arguments[2])
        }
        var klass = new Klass(1, 2, 3) 
    })()

    3

    /**
     * 当你读取一个对象的属性时,JS首先会在本地对象中查找这个属性,
     * 如果没有找到,JS开始在对象的原型中查找,
     * 仍未找到还会继续查找原型的原型,
     * 直到查找到Object。prototype
     * 如果找到这个属性,则返回这个值,否则返回undefined
     */
    var Person = function() {
        this.say = function() {
            console.log('e')
        }
    }
    Person.prototype.say = function() {
        console.log('ee')
    }
    new Person().say() // e
  • 相关阅读:
    求10个随机数的最大值、最小值、和、平均值
    设计并编写代码自动格斗类游戏
    用while实现阶乘
    安卓第三次作业
    第二次作业
    第一次作业
    第四次作业
    dialog
    用画图的方法理解原型对象和原型链,事半功倍今晚不加班
    【学习笔记】浅析Promise函数
  • 原文地址:https://www.cnblogs.com/jzm17173/p/3484979.html
Copyright © 2011-2022 走看看