Home > Web Front-end > JS Tutorial > Problems using inline functions in JavaScript_javascript tips

Problems using inline functions in JavaScript_javascript tips

WBOY
Release: 2016-05-16 19:17:54
Original
1334 people have browsed it

前段时间被IE和JavaScript脚本引擎的Memory Leak问题弄得郁闷坏了,不过幸好现在总算是柳暗花明了,并且找到了一些IE中使用脚本避免ML问题的方法。继续研究JavaScript的编写,有发现一些不算ML问题,但是可以节约IE内存使用的方法,在此和大家讨论讨论。

    我们在JavaScript中编写代码,对于定义函数的语句:

Problems using inline functions in JavaScript_javascript tipsfunction foo()
Problems using inline functions in JavaScript_javascript tips{
Problems using inline functions in JavaScript_javascript tips    
// TODO: . . .
Problems using inline functions in JavaScript_javascript tips
    return x;
Problems using inline functions in JavaScript_javascript tips}

可以说是在熟悉不过了。当然除了这种定义函数的方法,我们还有另外几种方法也能定义函数:

Problems using inline functions in JavaScript_javascript tipsvar foo = function()
Problems using inline functions in JavaScript_javascript tips{
Problems using inline functions in JavaScript_javascript tips    
// TODO: . . .
Problems using inline functions in JavaScript_javascript tips
    return x;
Problems using inline functions in JavaScript_javascript tips}

Problems using inline functions in JavaScript_javascript tipsvar foo = new Function('{/*todo*/return x;}');

    后两种方法定义的JavaScript函数,在调用起来和第一种没有任何效果上的区别。

    不过由于JavaScript是解释性语言,当我们定义一个函数的时候,解析引擎生成一个Function对象实例,然后把函数内容保存下来。所以每执行一次函数定义语句,就会生成一个函数。而不像编译语言,一个函数编译一次后就被任何语句调用。啊?难道JavaScript不能调用定义好的函数?不是这个意思了,当我们在制作JavaScript控件时,如果动态输出DHTML来作为控件的内容,就容易出现这样的问题。比如我们在一个HTML对象生成过程中,使用了inline方式定义的函数,那么这个元素生成几次,那个函数也就要同时生成几次。

Problems using inline functions in JavaScript_javascript tips function TestObject.prototype.Render(doc, id)
Problems using inline functions in JavaScript_javascript tips {
Problems using inline functions in JavaScript_javascript tips    
var span = doc.createElement('SPAN');
Problems using inline functions in JavaScript_javascript tips    span.Object 
= this;
Problems using inline functions in JavaScript_javascript tips    
this.m_Element = span;
Problems using inline functions in JavaScript_javascript tips
Problems using inline functions in JavaScript_javascript tips    
if ( id == "NamedMethod" )
Problems using inline functions in JavaScript_javascript tips    {
Problems using inline functions in JavaScript_javascript tips        span.onclick 
= asdf;
Problems using inline functions in JavaScript_javascript tips    }
Problems using inline functions in JavaScript_javascript tips    
else
Problems using inline functions in JavaScript_javascript tips    { 
Problems using inline functions in JavaScript_javascript tips        span.onclick 
= function()
Problems using inline functions in JavaScript_javascript tips        {
Problems using inline functions in JavaScript_javascript tips            
var asdf01 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips            
var asdf02 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips            
var asdf03 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips            
var asdf04 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips            
var asdf05 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips            
var asdf06 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips            
var asdf07 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips            
var asdf08 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips            
var asdf09 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips            
var asdf10 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips            
var asdf11 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips            
var asdf12 = ['a', 's', 'd', 'f'];
Problems using inline functions in JavaScript_javascript tips        };
Problems using inline functions in JavaScript_javascript tips    }
Problems using inline functions in JavaScript_javascript tips    span.Name 
= this.m_Description;
Problems using inline functions in JavaScript_javascript tips    span.innerText 
= this.m_Name;
Problems using inline functions in JavaScript_javascript tips    span.style.display 
= 'block';
Problems using inline functions in JavaScript_javascript tips    
return span;
Problems using inline functions in JavaScript_javascript tips }

    函数span.onclick = function()中的内容是用来占位置的,这样inline方式定义函数,每次Render()都就会生成一个新的函数对象。使用inline方式有什么不好呢?当对象实例多了的时候,会很明显的浪费内存空间呀,试验数据如下:

   
 Normal Method   Inline Method 
 Initialized  27.4 M 27.4 M
 Rendered  33.4 M 35.2 M

// Amount of memory consumed by IE (PM VM)

There is not much difference in absolute memory consumption alone, but if you look at relative memory consumption: (35.2 -33.4)/(33.4-27.4) = 30% !!!, which is still very impressive, and the larger the method itself, the more redundant data will be inline.

    附测试代码:
Problems using inline functions in JavaScript_javascript tips<html>
Problems using inline functions in JavaScript_javascript tips
<head>
Problems using inline functions in JavaScript_javascript tips    
<title>JScript Function Spendingtitle>
Problems using inline functions in JavaScript_javascript tips    
<meta name="author" content="birdshome@博客园" /> 
Problems using inline functions in JavaScript_javascript tips
head>
Problems using inline functions in JavaScript_javascript tips
<body onunload="ReleaseElements()">
Problems using inline functions in JavaScript_javascript tips    
<button id="NamedMethod" onclick="GenerateObjects(this)">
Problems using inline functions in JavaScript_javascript tips        Append Normal Elements
button>
Problems using inline functions in JavaScript_javascript tips    
<button id="AnonymousMethod" onclick="GenerateObjects(this)">
Problems using inline functions in JavaScript_javascript tips        Append Inline Elements
button>
Problems using inline functions in JavaScript_javascript tips    
<div id="container">
Problems using inline functions in JavaScript_javascript tips    
div>
Problems using inline functions in JavaScript_javascript tipsProblems using inline functions in JavaScript_javascript tips    
<script language="Javascript">
Problems using inline functions in JavaScript_javascript tips
function GenerateObjects(elmt)
Problems using inline functions in JavaScript_javascript tipsProblems using inline functions in JavaScript_javascript tips
{
Problems using inline functions in JavaScript_javascript tips    
var room = document.getElementById('container');
Problems using inline functions in JavaScript_javascript tips    
for ( var i=0 ; i < 1000 ; ++i )
    
{
         
var obj = new TestObject('__Object__'   i);
         room.appendChild(obj.Render(document, elmt.id));
    }
 
}


function TestObject(name)
{
this.m_Name = name;
this.m_Description = '';
this.m_Element = null;

this.toString = function()
{
return '[class TestObject]';
    }

}


function TestObject.prototype.Render(doc, id)
{
var span = doc.createElement('SPAN');
span.Object
= this;
this.m_Element = span;

if ( id == "NamedMethod" )
{
span.onclick
= asdf;
    }

else
{
span.onclick
= function()
{
var asdf01 = ['a', 's', 'd', 'f'];
var asdf02 = ['a', 's', 'd', 'f'];
var asdf03 = ['a', 's', 'd', 'f'];
var asdf04 = ['a', 's', 'd', 'f'];
var asdf05 = ['a', 's', 'd', 'f'];
var asdf06 = ['a', 's', 'd', 'f'];
var asdf07 = ['a', 's', 'd', 'f'];
var asdf08 = ['a', 's', 'd', 'f'];
var asdf09 = ['a', 's', 'd', 'f'];
var asdf10 = ['a', 's', 'd', 'f'];
var asdf11 = ['a', 's', 'd', 'f'];
var asdf12 = ['a', 's', 'd', 'f'];
}
;
    }

    span.Name 
= this.m_Description;
    span.innerText 
= this.m_Name;
    span.style.display 
= 'block';
    
return span;
}


function asdf()
{
var asdf01 = ['a', 's', 'd', 'f'];
var asdf02 = ['a', 's', 'd', 'f'];
var asdf03 = ['a', 's', 'd', 'f'];
var asdf04 = ['a', 's', 'd', 'f'];
var asdf05 = ['a', 's', 'd', 'f'];
var asdf06 = ['a', 's', 'd', 'f'];
var asdf07 = ['a', 's', 'd', 'f'];
var asdf08 = ['a', 's', 'd', 'f'];
    
var asdf09 = ['a', 's', 'd', 'f'];
    
var asdf10 = ['a', 's', 'd', 'f'];
    
var asdf11 = ['a', 's', 'd', 'f'];
    
var asdf12 = ['a', 's', 'd', 'f'];
}

script>
Problems using inline functions in JavaScript_javascript tipsProblems using inline functions in JavaScript_javascript tips    
<script language="javascript">
Problems using inline functions in JavaScript_javascript tips
function ReleaseElements()
Problems using inline functions in JavaScript_javascript tipsProblems using inline functions in JavaScript_javascript tips
{
Problems using inline functions in JavaScript_javascript tips    
var room = document.getElementById('container');
Problems using inline functions in JavaScript_javascript tips    
var spans = room.all.tags('SPAN');
Problems using inline functions in JavaScript_javascript tips    
for ( var i=0 ; i < spans.length ; ++i )
{
spans[i].Object
= '';
    }

}

script>
Problems using inline functions in JavaScript_javascript tips
body>
Problems using inline functions in JavaScript_javascript tips
html>
Problems using inline functions in JavaScript_javascript tips

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