zoukankan      html  css  js  c++  java
  • javascript基础拾遗(七)

    1.对象的继承__proto__

    var Language = {
            name: 'program',
            score: 8.0,
            popular: function () {
                return this.score/10*100 + '%';
            }
        }
        var Python = {
            name: 'python',
            score: 9.0
        }
        Python.__proto__ = Language
        console.log(Python.popular())
    

    运行结果: 90%
    __proto__属性将Python对象的原型指向Language对象,这样Python对象就像是从Language对象继承的一样。

    2.对象继承的另一种方法Object.create()

    var Language = {
            name: 'program',
            score: 8.0,
            popular: function () {
                return this.score/10*100 + '%';
            }
        }
        Python = Object.create(Language)
        Python.name = 'python'
        Python.score = 9.0
        console.log(Python.popular())
    

    运行结果: 90%

    3.原型链
    当访问一个对象的属性时,javascript引擎先在当前对象上查找该属性,如果没有,就到它的原型对象上找,
    如果还没有找到,就一直上溯到Object.prototype对象。
    例如,创建Array对象:
    var arr = [1,2,3];
    其原型链是:
    arr--->Array.prototype---->Object.prototype

    Array.prototype定义了indexof(),shift()等方法,因此可以直接在Array对象上调用这些函数。

    函数也是一个对象,它的原型链是:
    foo--->Function.prototype---->Object.prototype---->null
    Function.prototype定义了apply()等方法,因此所有函数都可以调用apply方法

    4.构造函数

    function Language(name){
            this.name = name
            this.score = 8.0
        }
        var language = new Language('python')
        console.log(language)
    

    Language是个普通的函数,new关键字调用Language函数,并返回Language对象,默认return this,省略
    Language函数this关键字也绑定到new创建的对象
    原型链如下:
    language--->Language.prototype---->Object.prototype--->null

    language有一个属性constructor,继承自Language,指向的是Language对象本身
    language.constructor == Language.prototype.constructor
    Language.prototype.constructor == Lang
    Object.getPrototypeOf(lang) == Lang.prototype
    language instanceOf Language

    function Language(name){
            this.name = name
            this.score = 8.0
    		this.popular = function(){
    			return this.score/10*100 + '%'
    		}
        }
        var language = new Language('python')
    	var language2 = new Language('C')
        console.log(language)
    	// false
    	console.log(language.popular == language2.popular)
    

    language和language2的popular方法不是同一个函数,根据属性查找原则,
    只需要将popular方法,添加到Language的原型上就可以了

    function Language(name){
            this.name = name
            this.score = 8.0
        }
        Language.prototype.popular = function () {
            return this.score/10*100 + '%'
        }
        var language = new Language('python')
    	var language2 = new Language('C')
        console.log(language)
    	// true
    	console.log(language.popular == language2.popular)
    

    需要注意的,构造函数首字母应该大写,普通函数首字母小写

  • 相关阅读:
    SQL Server 2008 如何查看与创建约束
    Hibernate的主键生成策略
    sql server的id字段设置为自动生成的,那么该怎么写insert语句呢?
    org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity() defined: com.bjsxt.model.Student.courses
    INSERT 语句与 FOREIGN KEY 约束"XXX"冲突。该冲突发生于数据库"XXX",表"XXX", column 'XXX。
    在排序数组中查找符合条件的数
    求二叉树中节点的最大距离
    设计包含min 函数的栈
    寻找链表的倒数第K个节点
    翻转句子中单词的顺序
  • 原文地址:https://www.cnblogs.com/shijingjing07/p/8000924.html
Copyright © 2011-2022 走看看