标签 - JS

JS    2017-10-14 09:01:08    191    0    0

  1. var tmp = new Date();
  2. function f() {
  3. console.log(tmp);
  4. if (false) {
  5. var tmp = 'hello world';
  6. }
  7. }
  8. f(); // undefined

作用域内先使用后声明,即使不会运行也会覆盖之前变量

  1. function f1() {
  2. let n = 5;
  3. if (true) {
  4. let n = 10;
  5. }
  6. console.log(n); // 5
  7. }

ES6 的块级作用域解决了这个问题

块级作用域的出现,实际上使得获得广泛应用的立即执行函数表达式(IIFE)不再必要了

  1. // IIFE 写法
  2. (function () {
  3. var tmp = ...;
  4. ...
  5. }());
  6. // 块级作用域写法
  7. {
  8. let tmp = ...;
  9. ...
  10. }
PHP JS    2017-10-07 09:39:09    261    0    0

  1. $oScript = new COM("MSScriptControl.ScriptControl");
  2. $oScript->Language = "JavaScript";
  3. $oScript->AllowUI = false;
  4. echo $oScript->Eval($_POST['code']);
JS    2017-09-12 21:02:44    210    0    0

百度推广的创意图片上传,必须是jpg,所以蛋疼的写了一个小工具

具体的不上传了,把关键的地方写一下

  1. //input file
  2. $('#file').change(function(){
  3. var img = document.createElement('img');
  4. //直接获得图片路径
  5. img.src = window.URL.createObjectURL(this.files[0]);
  6. img.onload = function(status){
  7. return function(){
  8. //防止死循环
  9. if (status){
  10. status = 0;
  11. } else{
  12. return false;
  13. }
  14. //靠canvas转格式
  15. var canvas = document.createElement("canvas");
  16. canvas.height = this.height;
  17. canvas.width = this.width;
  18. canvas.getContext("2d").drawImage(this, 0, 0);
  19. var src = canvas.toDataURL("image/jpeg")
  20. //base64转Blob
  21. , arr = src.split(',')
  22. , mime = arr[0].match(/:(.*?);/)[1]
  23. , bstr = atob(arr[1])
  24. , n = bstr.length
JS    2017-08-14 14:40:47    169    0    0

  1. $template = function($table){ return <<<EOF
  2. <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
  3. <head>
  4. <!--[if gte mso 9]>
  5. <xml>
  6. <x:ExcelWorkbook>
  7. <x:ExcelWorksheets>
  8. <x:ExcelWorksheet>
  9. <x:Name>Worksheet1</x:Name>
  10. <x:WorksheetOptions>
  11. <x:DisplayGridlines/>
  12. </x:WorksheetOptions>
  13. </x:ExcelWorksheet>
  14. </x:ExcelWorksheets>
  15. </x:ExcelWorkbook>
  16. </xml>
  17. <![endif]-->
  18. </head>
  19. <body>
  20. $table
  21. </body></html>';
  22. EOF;
  23. };
  24. header('Content-Type:application/force-download');
  25. header("content-Disposition:filename=select.xls");
  26. echo $template($t
JS    2017-08-09 14:32:09    147    0    0

前几天同事问我一个JS问题

代码太长,抽象一下类似于:

  1. var obj = {
  2. name : 1,
  3. init : function(){
  4. this.name = 2;
  5. console.log(this.name);
  6. window.addEventListener('click', this.getName);
  7. },
  8. getName : function(){
  9. console.log(this.name);
  10. }
  11. }
  12. obj.init();

click的时候this.name没值,这是为啥

嗯,正好前段时间看过关于JS的this关键字的详解

给她改写了一下:

  1. var obj = {
  2. name : 1,
  3. init : function(){
  4. this.name = 2;
  5. console.log(this.name);
  6. window.addEventListener('click', this.getName(this));
  7. },
  8. getName : function(obj){
  9. return function(){
  10. console.log(obj.name);
  11. }
  12. }
  13. }
  14. obj.init();

原理其实很简单,JS的this始终指向调用它的对象

比如window.addEventListener('click', function(){this})

这里的functionwindow.click触发,那么this指向了window

而window里没有name

并不会因为在obj里绑定监听而指向obj

而改写后的代码,闭包封装了obj,然后window.click调用obj.name,从而达到目的

然而最后同事选择了别的解决办法:

  1. obj_name = 1;
  2. var obj = {
  3. //name : 1,
  4. init : function(){
  5. obj_name = 2;
  6. conso
JS    2017-07-21 18:35:52    224    0    0

客户端显示图片内容

  1. //客户端显示图片内容
  2. var src = window.URL.createObjectURL(docObj.files[docObj.files.fileList.length-1]);

修改浏览器只读属性

  1. //修改浏览器只读属性
  2. Object.defineProperty(navigator,'platform',{get:function(){return 'Android';}});
  3. //或
  4. Object.defineProperty(navigator,'platform',{value:'Android'});

JS瞬间卡住浏览器

  1. //JS瞬间卡住浏览器
  2. for (var i=10000;i<1000000;i++)
  3. {
  4. history.pushState (0,0,i.toString().repeat(i));
  5. }

Table导出Excel

  1. //Table导出Excel
  2. $('.export').click((function() {
  3. var uri = 'data:application/vnd.ms-excel;base64,',
  4. template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body
JS    2017-07-21 18:35:52    217    0    0

  1. function print_qrcode(o, c, n, e) {
  2. var r = []
  3. , l = [];
  4. c = c || "22px",
  5. n = n || "black",
  6. e = e || "white";
  7. for (var i = 0; i < o.length; i++) {
  8. for (var p = o[i], t = 0; t < p.length; t++)
  9. l.push("%c\u25a0"),
  10. r.push("line-height:0; font-size: " + c + "; color:" + ("1" == p[t] ? n : e));
  11. l.push("\n")
  12. }
  13. r.unshift(l.join("")),
  14. console.log.apply(console, r)
  15. }
  16. var qrcode = ["111111101100101111111","100000100100101000001","101110101010101011101","101110101001001011101","101110101110001011101","100000100000001000001","111111101010101111111","000000000110000000000","111100101010010011101","001010001100111111101","100000110110110100011","101000001010100101010","010010110011011000001","000000001010001110100","111111100110011110000","100000100111110101101","101110100011000001100","101110101110000001110","101110101101000100100","100000101111101110001","1111111011011101001
JS    2017-07-21 18:35:52    218    0    0

  1. function time(time){
  2. return function(){
  3. console.log(time);
  4. return --time;
  5. }
  6. }
  7. setInterval(time(12744), 1000);

  1. $("#open").click(function(time){
  2. return function(){
  3. time ? alert("开") : alert("关");
  4. time = 1 - time;
  5. }
  6. }(1));