栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在JavaScript中制作课程?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在JavaScript中制作课程?

通过示例解释 构造对象 中不同事物的行为:

// Defined as a variable from an anonymous function// so that there is scope closure over variables// shared across all instances and the prototype.// If this isn't important, you don't need to close// scope around it, so define directlyvar ConstructedObject = (function constructorCreator () {    // Define any variables/methods to be shared across    // all instances but not polluting the namespace    var sharedVariable = 'foo';    // Next the actual constructor    function ConstructedObject () {        // Variables here are normally used to help        // each instance and will be kept in memory as        // long as the instance exists        var instanceVariable = 'bar';        // instance-specific properties get defined        // using the "this" keyword, these are the        // properties expected to be changed across        // each different instance        this.instanceProperty = true;        this.instanceMethod = function () { return instanceVariable; };        this.changeInstanceVar = function () { instanceVariable = 'foo'; }; // you do have access to the shared // variables here if you need them.    }    // After the constructor, you set up the    // prototype, if any. This is an object of shared    // properties and methods to be inherited by every    // instance made by the constructor, and it also    // inherits the prototype's prototype, too.    // Lets use a literal object for simplicity.    ConstructedObject.prototype = {        // Accessing the instance to which a method        // applies is done using the "this" keyword,        // similar to in the constructor        sharedMethod : function () { return [sharedVariable, this.instanceMethod(),this.instanceProperty]; },        changeSharedVar : function () { sharedVariable = 'bar'; }        // properties may also be defined    };    // Finally, the constructor is returned so it    // can be kept alive outside of the anonymous    // function used to create it    return ConstructedObject;// and the anonymous function is called to execute// what we've done so far})();

执行上述代码后,您将拥有一个构造器,该构造器创建具有实例特定变量和共享变量的对象。现在,通过创建两个实例并在进行一些更改之前和之后进行比较来查看它们的行为。

// First create the two instancesvar myObjA = new ConstructedObject(),    myObjB = new ConstructedObject();// Now compare them, the sharedMethod method we// used in the prototype offers an easy way to// do thisconsole.log( myObjA.sharedMethod(), myObjB.sharedMethod() );// ["foo", "bar", true] ["foo", "bar", true]// Next lets change the different variables in// myObjB so we can see what happens, again the// change* methods defined before let us do this// easilymyObjB.changeInstanceVar();myObjB.changeSharedVar();// For completeness, lets also change the property// on myObjB.myObjB.instanceProperty = false;// Now when we compare them again, we see that our// changes to the myObjB instance have only changed// the shared variables of myObjAconsole.log( myObjA.sharedMethod(), myObjB.sharedMethod() );// ["bar", "bar", true] ["bar", "foo", false]

这是两个已记录的语句,以便于查看

//     myObjA    myObjB["foo", "bar", true] ["foo", "bar", true]["bar", "bar", true] ["bar", "foo", false]


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/668849.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号