/    Sign up×
Community /Pin to ProfileBookmark

explain this function new function() {

please explain what type of this function is ,

new function() {
// do some thing
}

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@pixelsrainauthorMar 08.2011 — how can i access this function and functions inside it.

[CODE]

new function() {

// $.fn.validate = validate() {};

$.fn.validate = {

init: function(o) {

if(o.name == 'username') { this.username(o) };

if(o.name == 'password') { this.password(o) };

if(o.name == 'email') { this.email(o) };

if(o.name == 'dob') { this.dob(o) };

},

username: function(o) {

var user = /[(*()[]+.,/?:;'"~\#$%^&<>)+]/;

<i> </i> if (!o.value.match(user)) {

<i> </i> doValidate(o);

<i> </i> } else {

<i> </i> doError(o,'no special characters allowed');

<i> </i> };

<i> </i> },

<i> </i> password: function(o) {

<i> </i> var pass = /[(*()[]+.,/?:;'"
~\#$%^&<>)+]/;

if (!o.value.match(pass)) {

doValidate(o);

} else {

doError(o,'no special characters allowed');

};

},

email: function(o) {

var email = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

if (o.value.match(email)) {

doSuccess(o);

} else {

doError(o,'not a valid email');

};

},

dob: function(o) {

var dob = /(0[1-9]|1[012])+/(0[1-9]|[12][0-9]|3[01])+/(19|20)dd/;

if (o.value.match(dob)) {

doSuccess(o);

} else {

doError(o,'not a valid date');

};

}

};



function doSuccess(o) {

$('#' + o.id + '_img').html('<img src="images/accept.gif" border="0" style="float:left;" />');

$('#' + o.id + '_li').removeClass("error");

$('#' + o.id + '_msg').html("");

$('#' + o.id + '_li').addClass("success");

}



function doError(o,m) {

$('#' + o.id + '_img').html('<img src="images/exclamation.gif" border="0" style="float:left;" />');

$('#' + o.id + '_li').addClass("error");

$('#' + o.id + '_msg').html(m);

$('#' + o.id + '_li').removeClass("success");

}

//private helper, validates each type after check

function doValidate(o) {

$('#' + o.id + '_img').html('<img src="images/loading.gif" border="0" style="float:left;" />');

$.post('ajax.php', { id: o.id, value: o.value }, function(json) {

eval("var args = " + json);

if (args.success == true)

{

doSuccess(args);

}

else

{

doError(args,args.msg);

}

});

};



};
[/CODE]
Copy linkTweet thisAlerts:
@KorMar 08.2011 — It is a constructor. It can create an object and give it pairs property:value
<i>
</i>&lt;script type="text/javascript"&gt;
var Employee= new function(){
this.firstname='Bob';
this.name='Dole';
}
alert(Employee.firstname);
alert(Employee.name);
&lt;/script&gt;

Usually the constructor is written otherwise:
<i>
</i>&lt;script type="text/javascript"&gt;
function Constructor(firstname,name){
this.firstname=firstname;
this.name=name;
}
var Employee=new Constructor('Bob','Dole');
alert(Employee.firstname);
alert(Employee.name);
&lt;/script&gt;
Copy linkTweet thisAlerts:
@pixelsrainauthorMar 08.2011 — thanks for reply. so could you explain main parts from this script

[CODE]


new function() {

// $.fn.validate = validate() {};

$.fn.validate = {

init: function(o) {

if(o.name == 'username') { this.username(o) };

if(o.name == 'password') { this.password(o) };

if(o.name == 'email') { this.email(o) };

if(o.name == 'dob') { this.dob(o) };

},

username: function(o) {

var user = /[(*()[]+.,/?:;'"~\#$%^&amp;&lt;&gt;)+]/;

<i> </i> if (!o.value.match(user)) {

<i> </i> doValidate(o);

<i> </i> } else {

<i> </i> doError(o,'no special characters allowed');

<i> </i> };

<i> </i> },

<i> </i> password: function(o) {

<i> </i> var pass = /[(*()[]+.,/?:;'"
~\#$%^&<>)+]/;

if (!o.value.match(pass)) {

doValidate(o);

} else {

doError(o,'no special characters allowed');

};

},

email: function(o) {

var email = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

if (o.value.match(email)) {

doSuccess(o);

} else {

doError(o,'not a valid email');

};

},

dob: function(o) {

var dob = /(0[1-9]|1[012])+/(0[1-9]|[12][0-9]|3[01])+/(19|20)dd/;

if (o.value.match(dob)) {

doSuccess(o);

} else {

doError(o,'not a valid date');

};

}

};



function doSuccess(o) {

$('#' + o.id + '_img').html('<img src="images/accept.gif" border="0" style="float:left;" />');

$('#' + o.id + '_li').removeClass("error");

$('#' + o.id + '_msg').html("");

$('#' + o.id + '_li').addClass("success");

}



function doError(o,m) {

$('#' + o.id + '_img').html('<img src="images/exclamation.gif" border="0" style="float:left;" />');

$('#' + o.id + '_li').addClass("error");

$('#' + o.id + '_msg').html(m);

$('#' + o.id + '_li').removeClass("success");

}

//private helper, validates each type after check

function doValidate(o) {

$('#' + o.id + '_img').html('<img src="images/loading.gif" border="0" style="float:left;" />');

$.post('ajax.php', { id: o.id, value: o.value }, function(json) {

eval("var args = " + json);

if (args.success == true)

{

doSuccess(args);

}

else

{

doError(args,args.msg);

}

});

};



};


[/CODE]
Copy linkTweet thisAlerts:
@KorMar 08.2011 — In that case, new function() creates an anonymous object (anonymous singleton). I guess it is useful in the debugging process. And keeps the inner codes in private scope.
<i>
</i>&lt;script type="text/javascript"&gt;
new function(){
this.property='somevalue'
this.method=function(){alert(this.property)}
this.method()
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@pixelsrainauthorMar 08.2011 — so i can assign this anonymous object to and other object. if yes how?
Copy linkTweet thisAlerts:
@pixelsrainauthorMar 08.2011 — what is the meaning of init: function(o) in the function .

why ' : ' use after init .
Copy linkTweet thisAlerts:
@KorMar 08.2011 — JSON:

www.json.org
×

Success!

Help @pixelsrain spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...