Home > Web Front-end > JS Tutorial > How to prevent library functions from being accidentally modified incorrectly

How to prevent library functions from being accidentally modified incorrectly

一个新手
Release: 2017-09-18 09:42:31
Original
1491 people have browsed it

When a function reads an inherited object, it actually reads the inherited value. If you assign values ​​to the properties of an inherited object, these properties will only affect the inherited object itself, not the original object

//inherit()返回了一个继承自原型对象p的属性的新对象
        //这里使用ECMAScript 5中的Object.create()函数(如果可用)
        //如果不可用,则使用其它方法
        function inherit(p) {
            if (p == null) throw TypeError();  //p是一个对象,但不能是null
            if (Object.create)            //如果Object.create()存在
                return Object.create(p);  //直接使用
            var t = typeof p;             //否则进一步检验
            if (t !== "object" && t !== "function") throw TypeError();  
            function f() { }; //定义一个空构造函数
            f.prototype = p; //将其原型属性设置未p
            return new f();  //使用f()创建p的继承对象
        }
Copy after login

The above is the detailed content of How to prevent library functions from being accidentally modified incorrectly. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template