how2j.cn


工具版本兼容问题
在JavaScript中可以自定义对象,添加新的属性,添加新的方法

示例 1 : 通过new Object创建对象   
示例 2 : 通过function设计一个对象   
示例 3 : 为已经存在的对象,增加新的方法   

示例 1 :

通过new Object创建对象

通过new Object()创建一个对象
"); window.frames["iframe_show1117"].document.write(decodeHtml($("textarea#stepcodeTextarea1117").val())); window.frames["iframe_show1117"].document.close(); $(window.frames["iframe_show1117"]).load(function(){ $("#iframe_show1117").height($("#iframe_show1117").contents().find("body").height()+showittryitheight); }); $("#iframe_show1117").height($("#iframe_show1117").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var hero = new Object(); hero.name = "盖伦"; //定义一个属性name,并且赋值 hero.kill = function(){ document.write(hero.name + " 正在杀敌" ); //定义一个函数kill } hero.kill(); //调用函数kill </script>
<script>
var hero = new Object();
hero.name = "盖伦"; //定义一个属性name,并且赋值
hero.kill = function(){
  document.write(hero.name + " 正在杀敌" ); //定义一个函数kill
}
 
hero.kill(); //调用函数kill
 
</script>
"); window.frames["iframe1117"].document.write(decodeHtml(code1117)); window.frames["iframe1117"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1117"]).load(function(){ $("#iframe1117").height($("#iframe1117").contents().find("body").height()+showittryitheight); }); $("#iframe1117").height($("#iframe1117").contents().find("body").height()+showittryitheight); alreadyWriteCode1117 = code1117; $("#rendering1117").hide(); $("#rendered1117").show(); } var tRereshRetry2DemoPanel1117 = setInterval(rereshRetry2DemoPanel1117,1000); var binded1117 = false; $("textarea#stepcodeTextarea1117").keyup(function(){ if(!binded1117){ $(window).bind('beforeunload',function(){ binded1117 = true; return "xxxx"; }); } var newCode = $(this).val() code1117 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1117!=newCode){ // window.frames["iframe1117"].document.write("
"); // window.frames["iframe1117"].document.write(decodeHtml($("textarea#stepcodeTextarea1117").val())); // window.frames["iframe1117"].document.close(); // $(window.frames["iframe1117"]).load(function(){ // $("#iframe1117").height($("#iframe1117").contents().find("body").height()+showittryitheight); // }); // code1117 = newCode; // } }); $(".tryButton1117").click(function(){ $("#tryDiv1117").show(); $("#stepcodeTextarea1117").focus(); $("#stepcodeTextarea1117").height(200); $("#iframe1117").height(0); window.frames["iframe1117"].document.write("
"); window.frames["iframe1117"].document.write(decodeHtml($("textarea#stepcodeTextarea1117").val())); window.frames["iframe1117"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1117"]).load(function(){ $("#iframe1117").height($("#iframe1117").contents().find("body").height()+showittryitheight); }); $("#iframe1117").height($("#iframe1117").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1117.focus(); editor1117.setSize(null, "250"); $("#rendering1117").hide(); $("#rendered1117").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1117 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1117"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1117.on("change",function(doc){ if(!binded1117){ $(window).bind('beforeunload',function(){ binded1117 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1117 = newCode; $("textarea#stepcodeTextarea1117").val(newCode); if(alreadyWriteCode1117!=code1117){ lastModifedTime1117 = new Date().getTime(); $("#rendering1117").show(); $("#rendered1117").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1117 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1117.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1117").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

通过function设计一个对象

通过new Object创建对象有个问题,就是每创建一个对象,都得重新定义属性和函数。这样代码的重用性不好
那么,采用另一种方式,通过function设计一种对象。 然后实例化它 。
这种思路很像Java里的设计一种类,但是 javascript没有类,只有对象,所以我们叫设计一种对象
"); window.frames["iframe_show1148"].document.write(decodeHtml($("textarea#stepcodeTextarea1148").val())); window.frames["iframe_show1148"].document.close(); $(window.frames["iframe_show1148"]).load(function(){ $("#iframe_show1148").height($("#iframe_show1148").contents().find("body").height()+showittryitheight); }); $("#iframe_show1148").height($("#iframe_show1148").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function Hero(name){ this.name = name; this.kill = function(){ document.write(this.name + "正在杀敌<br>"); } } var gareen = new Hero("盖伦"); gareen.kill(); var teemo = new Hero("提莫"); teemo.kill(); </script>
<script>
function Hero(name){
  this.name = name;
  this.kill = function(){
     document.write(this.name + "正在杀敌<br>");
  }
}

var gareen = new Hero("盖伦");
gareen.kill();

var teemo = new Hero("提莫");
teemo.kill();
 
</script>
"); window.frames["iframe1148"].document.write(decodeHtml(code1148)); window.frames["iframe1148"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1148"]).load(function(){ $("#iframe1148").height($("#iframe1148").contents().find("body").height()+showittryitheight); }); $("#iframe1148").height($("#iframe1148").contents().find("body").height()+showittryitheight); alreadyWriteCode1148 = code1148; $("#rendering1148").hide(); $("#rendered1148").show(); } var tRereshRetry2DemoPanel1148 = setInterval(rereshRetry2DemoPanel1148,1000); var binded1148 = false; $("textarea#stepcodeTextarea1148").keyup(function(){ if(!binded1148){ $(window).bind('beforeunload',function(){ binded1148 = true; return "xxxx"; }); } var newCode = $(this).val() code1148 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1148!=newCode){ // window.frames["iframe1148"].document.write("
"); // window.frames["iframe1148"].document.write(decodeHtml($("textarea#stepcodeTextarea1148").val())); // window.frames["iframe1148"].document.close(); // $(window.frames["iframe1148"]).load(function(){ // $("#iframe1148").height($("#iframe1148").contents().find("body").height()+showittryitheight); // }); // code1148 = newCode; // } }); $(".tryButton1148").click(function(){ $("#tryDiv1148").show(); $("#stepcodeTextarea1148").focus(); $("#stepcodeTextarea1148").height(200); $("#iframe1148").height(0); window.frames["iframe1148"].document.write("
"); window.frames["iframe1148"].document.write(decodeHtml($("textarea#stepcodeTextarea1148").val())); window.frames["iframe1148"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1148"]).load(function(){ $("#iframe1148").height($("#iframe1148").contents().find("body").height()+showittryitheight); }); $("#iframe1148").height($("#iframe1148").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1148.focus(); editor1148.setSize(null, "250"); $("#rendering1148").hide(); $("#rendered1148").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1148 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1148"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1148.on("change",function(doc){ if(!binded1148){ $(window).bind('beforeunload',function(){ binded1148 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1148 = newCode; $("textarea#stepcodeTextarea1148").val(newCode); if(alreadyWriteCode1148!=code1148){ lastModifedTime1148 = new Date().getTime(); $("#rendering1148").show(); $("#rendered1148").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1148 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1148.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1148").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

为已经存在的对象,增加新的方法

现在Hero对象已经设计好了,但是我们发现需要追加一个新的方法keng(),那么就需要通过prototype实现这一点
"); window.frames["iframe_show1149"].document.write(decodeHtml($("textarea#stepcodeTextarea1149").val())); window.frames["iframe_show1149"].document.close(); $(window.frames["iframe_show1149"]).load(function(){ $("#iframe_show1149").height($("#iframe_show1149").contents().find("body").height()+showittryitheight); }); $("#iframe_show1149").height($("#iframe_show1149").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> function Hero(name){ this.name = name; this.kill = function(){ document.write(this.name + "正在杀敌<br>"); } } var gareen = new Hero("盖伦"); gareen.kill(); var teemo = new Hero("提莫"); teemo.kill(); Hero.prototype.keng = function(){ document.write(this.name + "正在坑队友<br>"); } gareen.keng(); teemo.keng(); </script>
"); window.frames["iframe1149"].document.write(decodeHtml(code1149)); window.frames["iframe1149"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1149"]).load(function(){ $("#iframe1149").height($("#iframe1149").contents().find("body").height()+showittryitheight); }); $("#iframe1149").height($("#iframe1149").contents().find("body").height()+showittryitheight); alreadyWriteCode1149 = code1149; $("#rendering1149").hide(); $("#rendered1149").show(); } var tRereshRetry2DemoPanel1149 = setInterval(rereshRetry2DemoPanel1149,1000); var binded1149 = false; $("textarea#stepcodeTextarea1149").keyup(function(){ if(!binded1149){ $(window).bind('beforeunload',function(){ binded1149 = true; return "xxxx"; }); } var newCode = $(this).val() code1149 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1149!=newCode){ // window.frames["iframe1149"].document.write("
"); // window.frames["iframe1149"].document.write(decodeHtml($("textarea#stepcodeTextarea1149").val())); // window.frames["iframe1149"].document.close(); // $(window.frames["iframe1149"]).load(function(){ // $("#iframe1149").height($("#iframe1149").contents().find("body").height()+showittryitheight); // }); // code1149 = newCode; // } }); $(".tryButton1149").click(function(){ $("#tryDiv1149").show(); $("#stepcodeTextarea1149").focus(); $("#stepcodeTextarea1149").height(200); $("#iframe1149").height(0); window.frames["iframe1149"].document.write("
"); window.frames["iframe1149"].document.write(decodeHtml($("textarea#stepcodeTextarea1149").val())); window.frames["iframe1149"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1149"]).load(function(){ $("#iframe1149").height($("#iframe1149").contents().find("body").height()+showittryitheight); }); $("#iframe1149").height($("#iframe1149").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1149.focus(); editor1149.setSize(null, "250"); $("#rendering1149").hide(); $("#rendered1149").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1149 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1149"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1149.on("change",function(doc){ if(!binded1149){ $(window).bind('beforeunload',function(){ binded1149 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1149 = newCode; $("textarea#stepcodeTextarea1149").val(newCode); if(alreadyWriteCode1149!=code1149){ lastModifedTime1149 = new Date().getTime(); $("#rendering1149").show(); $("#rendered1149").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1149 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1149.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1149").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-JavaScript-自定义对象 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图