I have two function in my javascript class where one function is called in another function, I have use the parameter how I use in other programming language. But it is throwing me
"SyntaxError: Unexpected identifier"
class IpSubnetMatch {
function ip2longConvert(ip)
{
var ponents;
if(ponents = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/))
{
var iplong = 0;
var power = 1;
for(var i=4; i>=1;i--)
{
iplong += power * parseInt(ponents[i]);
power *= 256;
}
return iplong;
}
else return -1;
}
function inSubNet(ip,subnet)
{
var mask, base_ip;
var long_ip = ip2longConvert(ip);
if((mask = subnet.match(/^(.*?)\/(\d{1,2})$/)) && ((base_ip = ip2longConvert(mask[1])) >= 0))
{
var freedom = Math.pow(2,32 - parseInt(mask[2]));
return(long_ip > base_ip) && (long_ip < base_ip + freedom -1);
}
else return false;
}
}
let user = new IpSubnetMatch();
user.inSubNet('10.1.5.5', '10.1.0.0/16');
I have two function in my javascript class where one function is called in another function, I have use the parameter how I use in other programming language. But it is throwing me
"SyntaxError: Unexpected identifier"
class IpSubnetMatch {
function ip2longConvert(ip)
{
var ponents;
if(ponents = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/))
{
var iplong = 0;
var power = 1;
for(var i=4; i>=1;i--)
{
iplong += power * parseInt(ponents[i]);
power *= 256;
}
return iplong;
}
else return -1;
}
function inSubNet(ip,subnet)
{
var mask, base_ip;
var long_ip = ip2longConvert(ip);
if((mask = subnet.match(/^(.*?)\/(\d{1,2})$/)) && ((base_ip = ip2longConvert(mask[1])) >= 0))
{
var freedom = Math.pow(2,32 - parseInt(mask[2]));
return(long_ip > base_ip) && (long_ip < base_ip + freedom -1);
}
else return false;
}
}
let user = new IpSubnetMatch();
user.inSubNet('10.1.5.5', '10.1.0.0/16');
Share
Improve this question
edited Apr 8, 2019 at 18:00
cyborg
asked Apr 8, 2019 at 16:25
cyborgcyborg
8781 gold badge15 silver badges36 bronze badges
1
- Normally you want to leave the original code so we can understand the nature of the question when someone reads it in the future. – ChaosPandion Commented Apr 8, 2019 at 17:59
3 Answers
Reset to default 2You need to define methods in the class. You also may want to define them as static since they really don't depend on any instance state.
class IpSubnetMatch {
ip2longConvert(ip) {
// ...
}
inSubNet(ip,subnet) {
const long_ip = this.ip2longConvert(ip);
// ...
}
}
Classes in JavaScript do not actually offer additional functionalities and are often described as providing "syntactical sugar" over prototypes and inheritance. ES6 Classes offer a cleaner and more elegant syntax.
class IpSubnetMatch {
constructor() {
}
ip2longConvert(ip) {
}
inSubNet(ip,subnet) {
//Call methods using this keyword.
this.ip2longConvert(ip);
}
}
Class methods do not use the function keyword in class syntax. Use this keyword to reference methods or properties.
The keyword function
is the problem. Since ECMA2015 you should use arrow functions.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745644563a4637892.html
评论列表(0条)